Page 519 - Introduction to Programming with Java: A Problem Solving Approach
P. 519
Employee class’s heading. Note that the Employee class defines just one instance variable, id. Does that mean that an Employee object has no name? No. Employee objects do have names because the Em- ployee class inherits the name instance variable from the Person superclass. Now you’ll learn how to access name from within the Employee class.
The Employee class’s display method is in charge of printing an employee’s information—name and id. Printing the id is easy because id is declared within the Employee class. Printing name requires a bit more work. Since name is a private instance variable in the Person superclass, the Employee class cannot access name directly (that’s the same interpretation of private that we’ve always had). But the Employee class can access name by calling the Person class’s public getName accessor method. Here’s the relevant code from the display method:
System.out.println("name: " + getName());
As you might recall, in an instance method, if you call a method that’s in the same class as the class you’re currently in, the reference variable dot prefix is unnecessary. Likewise, in an instance method, if you call a method that’s in the superclass of the class you’re currently in, the reference variable dot prefix is unneces- sary. Thus, there’s no reference variable dot prefix in the above call to getName.
12.5 Constructors in a Subclass
Let’s now examine Figure 12.11’s two-parameter Employee constructor. The goal is to assign the passed-
in name and id values to the associated instance variables in the instantiated Employee object. Assigning
to the id instance variable is easy because id is declared within the Employee class. But assigning to the Apago PDF Enhancer
name instance variable is harder because name is a private instance variable in the Person superclass. There’s no setName mutator method in Person, so how does name get set? Read on. . . .
Using super to Call a Superclass Constructor
Employee objects inherit the name instance variable from Person. It follows that Employee objects should use the Person constructor to initialize their inherited name instance variables. But how can an Employee object call a Person constructor? It’s easy—once you know how. To call a superclass con- structor, use the reserved word super followed by parentheses and a comma-separated list of arguments that you want to pass to the constructor. For example, here’s how Figure 12.11’s Employee constructor calls the one-parameter Person constructor:
super(name);
Calls to super are allowed only in one particular place. They’re allowed only from within a constructor, and they must be the first line within a constructor. That should sound familiar. In Chapter 7, you learned another usage for the keyword this, a usage that is distinct from using this dot to specify an instance member. The syntax for this other usage of this is:
this(<arguments>);
This kind of this usage calls another (overloaded) constructor from within a constructor in the same class. And recall that you must make such a call on the first line of your constructor.
By the way, would it be legal to have a this constructor call and a super constructor call within the same constructor? No, because with both constructor calls in the same constructor, that means only one of the constructor calls can be in the first line. The other one would violate the rule that constructor calls must be in the first line.
12.5 Constructors in a Subclass 485