Page 520 - Introduction to Programming with Java: A Problem Solving Approach
P. 520
486 Chapter 12 Aggregation, Composition, and Inheritance Default Call to Superclass Constructor
The Java designers at Sun are fond of calling superclass constructors since doing so promotes software re- use. If you write a subclass constructor and don’t include a call to another constructor (with this or with super), the Java compiler sneaks in and inserts a superclass zero-parameter constructor call by default. Thus, although Figure 12.11’s Employee zero-parameter constructor has an empty body, the Java compiler automatically inserts super(); in it. So these two constructors are functionally equivalent:
public Employee()
{}
public Employee()
{
}
super();
The explicit super(); call makes it clear what’s going on. Feel free to include it if you wish, to make your code more self-documenting.
Whenever a constructor is called, the JVM automatically runs up the hierarchical tree to the greatest
grandparent’s constructor, and it executes that greatest grandparent’s constructor first. Then it executes the
code in the constructor below it, and so on, and finally it executes the rest of the code in the originally called
2
12.6 Method Overriding
Apago PDF Enhancer
From Chapter 7, you know about method overloading—that’s when a single class contains two or more methods with the same name but a different sequence of parameter types. Now for a related concept— method overriding. That’s when a subclass has a method with the same name, the same sequence of param- eter types, and the same return type as a method in a superclass. The term “overriding” should make sense when you realize that an overriding method overrides/supersedes its associated superclass method. That means, by default, an object of the subclass uses the subclass’s overriding method and not the superclass’s overridden method.
The concept of a subclass object using the subclass’s method rather than the superclass’s method falls in line with this general principle of programming: Local stuff takes precedence over global stuff. Can you think of where else this rule applies? If a local variable and an instance variable have the same name, the lo- cal variable takes precedence when you’re inside the local variable’s method. The same reasoning applies to parameters taking precedence over instance variables when you’re inside the parameter’s method.
Method Overriding Example
To explain method overriding, we’ll continue with the implementation of the Person/Employee/FullTime program. We implemented the Person and Employee classes in Section 12.4. We implement the FullTime class in Figure 12.12. Note FullTime’s display method. It has the same sequence of pa- rameter types as the display method in the Employee class of Figure 12.11. Since the FullTime class
2 This sequence is the same as the sequence that occurs naturally in the embryonic development of a living creature. The characteris- tics that develop first are the most ancient ones.
constructor.