Page 516 - Introduction to Programming with Java: A Problem Solving Approach
P. 516

                482 Chapter 12 Aggregation, Composition, and Inheritance
class diagrams use a solid line and a hollow arrow for inheritance relationships, with the arrow pointing to
the superclass. In Figure 12.9, note how the arrows do indeed point to the superclasses.
Inheritance Terminology
Unfortunately, the terms superclass and subclass can be misleading. The “super” in superclass seems to imply that superclasses have more capability, and the “sub” in subclass seems to imply that subclasses have less capability. Actually, it’s the other way around—subclasses have more capability. Subclasses can do ev- erything that superclasses can do, plus more.
For the most part, we’ll stick with the terms superclass and subclass since those are the formal terms used by Sun, but be aware that there is alternative terminology. Programmers often use the terms parent class or base class when referring to a superclass. And they often use the terms child class or derived class when re- ferring to a subclass. The parent-child relationship between classes is important because it determines inheri- tance. With a human parent-child relationship, the child normally inherits money from the parent.1 The class parent-child relationship parallels the human parent-child relationship. But with a class parent-child relation- ship, the child doesn’t inherit money; instead it inherits the variables and methods defined in the superclass.
There are two more inheritance-related terms that you should be aware of. An ancestor class refers to any of the classes above a particular class in an inheritance hierarchy. For example, in Figure 12.9’s inheri- tance hierarchy, Employee and Person are the ancestors of FullTime. A descendant class refers to any of the classes below a particular class in an inheritance hierarchy. For example, in Figure 12.9’s inheritance hierarchy, Employee, Customer, FullTime, and PartTime are descendants of Person.
Benefits of Inheritance Apago PDF Enhancer
Long before reading this chapter, you were already convinced of the benefit of modeling your programs with classes, right? (In case you need to be reminded why you love classes so much, it’s because classes allow you to encapsulate things.) So you should be able to see the benefit of having a Customer class and also an Employee class for a department store program. OK, having separate Customer and Employee classes is good, but why stir up trouble and give them a superclass? If there’s no superclass for the Customer and Employee classes, then the things common to customers and employees would have to be defined in both classes. For example, you’d need a name instance variable and a getName method in both classes. But redundant code is almost always a bad idea. Why? With redundant code, debugging and upgrading chores are more tedious. After fixing or improving the code in one place, the programmer must remember to fix or improve the code in the other place as well.
In Figure 12.9, notice that classes at different levels in the hierarchy contain different instance vari- ables, and they have different methods (although Employee and FullTime both have a display method, the methods are different; that is, they behave differently). There is no functional duplication, and there is maximal code reusability. Code reusability is when you have code that provides functionality for more than one part of a program. Putting common code from two classes into a superclass is one example of code reusability. Code reusability can also take place when you want to add a significant chunk of function- ality to an existing class. You might want to implement the functionality by adding code directly to the ex-
isting class. But suppose the class works perfectly, and you’re scared to touch it for fear of messing it up. Or maybe your know-it-all co-worker wrote the class, and you don’t want to risk getting him/her riled up over code modifications. No problem. Extend the class (that is, create a subclass) and implement the new functionality in the extended class.
 Don’t try to teach old dogs new tricks.
       1 Author John hopes that author/father Ray shares this sentence’s sentiment.






















































































   514   515   516   517   518