Java Reference
In-Depth Information
Figure 6.4
The output of the SuperDemo program.
public class SuperDemo
{
public static void main(String [] args)
{
System.out.println(“Instantiating an Employee”);
Employee e = new Employee();
e.name = “Robert Smith”;
e.address = “111 Java Street”;
e.SSN = 999001111;
e.number = 1;
System.out.println(“Instantiating a Salary”);
Salary s = new Salary();
s.name = “Jane Smith”;
s.address = “222 Oak Drive”;
s.SSN = 111009999;
s.number = 2;
s.salary = 100000.00F;
System.out.println(“*** Invoking mailCheck on e ***”);
e.mailCheck();
System.out.println();
System.out.println(“*** Invoking mailCheck on s ***”);
s.mailCheck();
}
}
The super keyword is required when a child method wants to invoke an
overridden method in the parent. For example, if the super keyword was
omitted in the mailCheck() method of the Salary class in Figure 6.4, the
compiler would add this. :
this.mailCheck()
The previous statement creates an infinite recursion because
this.mailCheck() invokes mailCheck() in the Salary class. Your program
would keep calling mailCheck() in Salary until it eventually crashed when
it ran out of memory.
Search WWH ::




Custom Search