So, I’m working on a homework assignment, and I’m having a hard time following some of the directions, I’ve pasted the assignment below:
Create a hierarchy of five classes, plus one class included as a variable inside:
- Person has four String variables: name, address, phone, email
- Student is a subclass to Person and has one additional int variable status which takes values of 1, 2, 3, or 4 representing freshman, sophomore, junior, senior
- MyDate has three int variables for year, month, and day
- Employee is a subclass to Person and has one String variable office, one int variable for salary, and one MyDate variable for dateHired
- Staff is a subclass to Employee and has one additional String variable for title
- Faculty is a subclass to Employee and has one additional String variable for rank which takes values of Professor, Associate Professor, Assistant Professor, Instructor, and Adjunct. The data for all six classes should be private.
As for methods, you can skip the normal setters and getters if you write a single constructor that has parameters for all data and override the toString( ) method. Constructors of subclasses should use the super class constructor. The toString( ) methods of subclasses should use the toString( ) method of their super class.
The part that throws me for a loop is the idea that a single constructor can be written that will cover all the necessary parameters for the setters and getters instead of writing them in each sub-class. Is this possible? And how so?
You need to use the constructor of the superclass whilst creating the subclass. So it should be:
Use the
super(/*params of super class*/)to invoke the constructor of the super class and instantiate the inherited attributes. Note that you can only call a superclass constructor as the first statement of a constructor. If you don’t call a superclass constructor explicitly, a call to super() (the default constructor of the superclass) is inserted automatically by the Java compiler.For calling the parent class’s toString() use:
Similarly write the constructors and toString() methods of all classes.