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?
Inheritance is a mechanism in which one class acquires the property of another class. For example, a child inherits the traits of his/her parents. With inheritance, we can reuse the fields and methods of the existing class. Hence, inheritance facilitates Reusability and is an important concept of OOPs.
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.
First of all address should be rather a separate object.
Secondly, Student
cannot be a (in)direct descendant of Person
, as student is a civic-status/job-role (it's even hard to define) and person is a synonyme for human being, which on the other hand is a specie. In other words you cannot say that student is a (inheritance) special case of person, but you can say that person has a (composition) civic-status of student.
If you take a look at properties of your classes you'll also notice that they do not represent the same thing. Each object should focus on one thing (single responsibility principle), while Person
object defines first and last name, age etc. (person-related properties) and CollegeEmployee
defines salary and the name of the department (job-related properties). Totally unrelated properties.
In a nutshell, a person is a really complex object and it's a horrible example for a person who is trying to understand the principles of the OO-design.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With