Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using the principle of inheritance, write a program that creates classes as depi

ID: 3713285 • Letter: U

Question

Using the principle of inheritance, write a program that creates classes as depicted on the figure; also you need to apply the following:

For the supper class “Community Member” create 3 variables (name, age, gender) and 3 methods( sayName( ), sayAge( ),                   say Gender ( ).

For Employee class; it is required to have a new variable and method such as “Nationality” and sayNathionality( ) .

For Student class; it is required to have a new variable and method such as “Student_ID” and say Student_ID ( ).

Creating 3 objects for each class to display:

The name (for all classes)

The nationality (for Employee class)

The student ID (Student class)

    

please send the orignal document for the program

Explanation / Answer

Program with comments:

Super class CommunityMember:

package com.chegg.inheritance;

//CommunityMember super class

public class CommunityMember {

//Declaring variables name, age, gender

String name;

int age;

String gender;

//Constructor for the class for initializing name, age and gender values for the object

public CommunityMember(String name,int age,String gender){

this.name = name;

this.age = age;

this.gender = gender;

}

//Method definition for sayName() to display name

public void sayName() {

System.out.println("Name is: "+name);

}

//Method definition for sayAge() to display age

public void sayAge() {

System.out.println("Age is: "+age);

}

//Method definition for sayGender() to display gender

public void sayGender() {

System.out.println("Gender is: "+gender);

}

}

Subclass Employee class:

package com.chegg.inheritance;

//Employee class which is a subclass of CommunityMember Class

public class Employee extends CommunityMember{

//Declaring variable Nationality

String Nationality;

//Constructor to initialize the name, age, gender and nationality values for the employee object

public Employee(String name, int age, String gender,String Nationality) {

//Calling the super class constructor using super keyword and initializing name,age and gender values

super(name, age, gender);

//Setting the nationality value

this.Nationality = Nationality;

}

//Method definition for sayNationality() which displays Nationality

public void sayNationality() {

System.out.println("Nationality is: "+Nationality);

}

}

Subclass Student:

package com.chegg.inheritance;

//Student class which is a subclass of CommunityMember

public class Student extends CommunityMember{

//Variable Student_ID declared

int Student_ID;

//Constructor to initialize name, age, gender, Student id for the student object

public Student(String name, int age, String gender,int Student_ID) {

//Calling the super class constructor using super keyword and initializing name,age and gender values

super(name, age, gender);

//Setting the Student_ID value

this.Student_ID = Student_ID;

}

//Method definition for sayStudent_ID() which displays student id

public void sayStudent_ID() {

System.out.println("Student ID is: "+Student_ID);

}

}

Main class - Driver class:

package com.chegg.inheritance;

//Main class Driver to start the program

public class Driver {

public static void main(String[] args) {

//Creating 3 objects obj1, obj2, obj3 for the CommunityMember, Employee and Student classes and passing the values to the constructor

CommunityMember obj1 = new CommunityMember("Adam", 25, "Male");

Employee obj2 = new Employee("Sally", 27, "Female", "British");

Student obj3 = new Student("John", 20, "Male", 12345);

//Displaying the name for all objects by calling the sayName() method of each object

obj1.sayName();

obj2.sayName();

obj3.sayName();

//Displaying the nationality of Employee object obj2 by calling sayNationality() method

obj2.sayNationality();

//Displaying the Student id of Student object obj3 by calling sayStudent_ID() method

obj3.sayStudent_ID();

}

}