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

                Figure 12.12
FullTime class, which illustrates method overriding
extends the Employee class, the FullTime class’s display method overrides the Employee class’s
display method.
Using super to Call an Overridden Method
Sometimes, an object of the subclass might need to call the superclass’s overridden method. To perform such a call, you need to prefix the method call with super and then a dot. For example, in Figure 12.12’s FullTime subclass, note how the display method calls the superclass’s display method with super.display();.
Now look again at that super.display() method call in Figure 12.12’s FullTime class. What do you suppose would happen if you forgot to prefix that method call with super dot? Without the pre- fix, display(); would call the display method in the current class, FullTime, not the display method in the superclass. In executing the FullTime class’s display method, the JVM would call the FullTime class’s display method again. This process would repeat in an infinite loop.
12.6 Method Overriding 487
 /*************************************************************
*
FullTime.java
Dean & Dean
The describes a full-time employee.
*************************************************************/
*
*
*
public class FullTime extends Employee
{
private double salary = 0.0;
//**********************************************************
public FullTime()
{}
public FullTime(String name, int id, double salary)
  {
  }
super(name, id);
this.salary = salary;
//**********************************************************
       Apago PDF Enhancer
    public void display()
This method overrides the display method defined in the Employee class.
{
  }
// end FullTime class
super.display();
System.out.printf(
"salary: $%,.0f\n", salary);
This calls the two-parameter Employee constructor.
 This calls the display method defined in the Employee class.
 }


























































   519   520   521   522   523