1 The classes:
.a People class – 3 data members (id, name, gender) & rfunctions.
.b Faculty class – a subclass of People, with new data members (department, years of service) & new functions (as needed).
People class |
Faculty class |
public class People {protected int id;protected String name;protected char gender;public People( ) { }public People(int i, String n, char g) {
id=i; name=n; gender=g; } public void getData( ) { System.out.print(“id=”+id+”, name= ” +name+”, gender=”+gender+”, “); } } |
public class Faculty extends People {protected String dept;protected int yos;public Faculty( ) { }public Faculty(int i, String n, char g, String d, int y) { super(i, n, g); dept=d; yos=y; }public void getData( ) {
super.getData( ); System.out.print(“dept=”+dept+ “, years of service=”+yos); } } |
Note: (#1) The getData function of the Faculty class overrides the getData function of its superclass. This is called function overriding (not function overloading). (#2) The getData of Faculty is calling the getData of People by using super.getData( ). This expression consists of the keyword super which identifies its superclass, & the function in the superclass to be called into action.
.c Student class – a subclass of People, with new data member GPA & new functions.
.d GStudent (GraduateStudent) class – a subclass of Student, with new data members (major, earned credits) & new functions.
Student class |
GStudent class |
public class Student extends People {protected double gpa;public Student( ) { }public Student(int i, String n, char g, double a){ super(i, n, g); gpa=a; }
public void getData( ) { super.getData( ); System.out.print(“GPA=”+gpa+”, “); } }; |
public class GStudent extends Student {protected String major;protected int credits;public GStudent( ) { }public GStudent(int i, String n, char g, double a, String m, int c){ super(i, n, g, a); major=m; credits=c; }
public void getData( ) { super.getData( ); System.out.print(“Major=”+major+”, credits”+credits); } }; |
.e E32 program class – to instantiate objects of the 4 data types.
Program class “E32” |
public class E32{public static void main(String [ ] args){Faculty f1 = new Faculty(111, “John Smith”, ‘M’, “CIS”, 12);Student s1 = new Student(999, “Susan Johnson”, ‘F’, 3.25);
GStudent g1 = new GStudent(555, “Jennifer Lopez”, ‘F’, 3.66, “CIS”, 22); f1.getData( ); System.out.println( ); s1.getData( ); System.out.println( ); g1.getData( ); System.out.println( ); } }
|
Output: |
Modify E32 program based on the following information:
1 Add a new data member DOB (or, date of birth) to the People class.
2 Modify the class function(s) to reflect the impact of adding a new data member.
3 Modify the sub-classes to reflect the changes occurred in the super class.
4 Add DOB data (of your choice) to all objects.
5 Display all objects.