For my homework, we’ve been tasked with “declare an array of four “regular” College Employees, three Faculty and seven Students. Prompt the user to specify which type of data will be entered (C,F,S) or the option to Quit (Q). While the user continues, accept data entry for the appropriate person. Display an error message if the user enters more than the specified number for each person type. When the user quits, display a report on the screen listing each group of persons under the appropriate heading. If the user has not entered data for one or more types of Person during a session display an appropriate message under the appropriate heading.”
Class | Extends | Variables
--------------------------------------------------------
Person | None | firstName, lastName, streetAddress, zipCode, phone
CollegeEmployee | Person | ssn, salary,deptName
Faculty | CollegeEmployee | tenure(boolean)
Student | person | GPA,major
After reading the Tutorials on inheritance and trolling a bunch of inheritance discussions, I think I’ve got it right on paper, but would prefer some input before I get elbows deep in code that doesn’t work. 🙂
I’m defining
Person[x] = new Student();
(or Faculty or CollegeEmployee).
The Person class has all the input fields for a Person, and the subclasses have ONLY the additional data (e.g., major in the case of Student).
When I create the new Student(); the input fields in BOTH the People and Student classes will be available to me because Student extends People and the additional variables defined in Student are appended to the definition of Person for that instance.
When it comes time to pull data from the array, Java sees it as an array of Person, so I need to add logic
if Person[x] instanceof Student (or `Faculty` or `CollegeEmployee`)
to execute the appropriate actions for the type of Person. My sense is that the instanceof is acting to override (in this case to append to) what Java knows about the Person class on the output side.
Am I missing any critical understandings of this?
There is not only inheritance, but polymorphism – just put code necessary to enter and validate object data in method of object (say: inputMyData() overriding base method in person, possibly calling method of superclass) – this way you can avoid instanceof and casting.