Java Reference
In-Depth Information
Listing 16-12.
An AOSuper Class
// AOSuper.java
package com.jdojo.inheritance;
public class AOSuper {
public void print() {
System.out.println("Inside AOSuper.print()");
}
}
The code in Listing 16-13 contains the declaration for an
AOSub
class, which inherits from the
AOSuper
class.
Listing 16-13.
An AOSub Class, Which Inherits from the AOSuper Class
// AOSub.java
package com.jdojo.inheritance;
public class AOSub extends AOSuper {
public void print() {
// Call print() method of AOSuper class
super.print();
// Print a message
System.out.println("Inside AOSub.print()");
}
public void callOverridenPrint() {
// Call print() method of AOSuper class
super.print();
}
}
The
AOSub
class overrides the
print()
method of the
AOSuper
class. Note the
super.print()
method call inside
the
print()
and the
callOverridenPrint()
methods of the
AOSub
class. It will call the
print()
method of
AOSuper
class. The output of Listing 16-14 shows that a method call with a
super
qualifier calls the overridden method in the
superclass.
Listing 16-14.
A Test Class to Test a Method Call with the super Qualifier
// AOTest.java
package com.jdojo.inheritance;
public class AOTest {
public static void main(String[] args) {
AOSub aoSub = new AOSub();
aoSub.print();
aoSub.callOverridenPrint();
}
}