Page 517 - Introduction to Programming with Java: A Problem Solving Approach
P. 517
12.4 Implementation of Person/Employee/FullTime Hierarchy 483
You’ve seen that inheritance gives rise to code reusability, and you should now be properly convinced of the benefits of code reusability. Another benefit of inheritance is that it gives rise to smaller modules (because classes are split into superclasses and subclasses). In general, smaller modules are good because there’s less code to wade through when searching for bugs or making upgrades.
12.4 Implementation of Person/Employee/ FullTime Hierarchy
To explain how to implement inheritance, we’ll implement the Person/Employee/FullTime hierar- chy shown in Figure 12.9. We’ll implement the Person and Employee classes in this section and the FullTime class in Section 12.6.
The Person class
Figure 12.10 contains an implementation of the Person class. It will be a superclass, but there’s no special code in the Person class that indicates it will be a superclass. The special code comes later when we define Person’s subclasses. That’s where we indicate that Person is a superclass for those subclasses.
/*******************************************************
*
Person.java
Dean & Dean
*
*
Apago PDF Enhancer
*
The is a base class for an inheritance hierarchy.
*******************************************************/
public class Person
{
private String name = "";
//****************************************************
⎫ public Person() ⎪
{} ⎪
⎪ public Person(String name) ⎬
⎪ {⎪
this.name = name; ⎪ }⎭
//****************************************************
public String getName()
}
// end Person class
{
}
return this.name;
Remember: Once you write your own constructor, the automatic zero-parameter default constructor disappears, and if you want one, you must write it explicitly.
Figure 12.10
Person class, superclass for the Employee class