A Second Use for super
The second form of super acts somewhat such as this, except in which it always refers to the superclass of the subclass in that it, is used. This usage has the subsequent general form:
super.member
Here member could be either a method or an instance variable.
This second form of super is most applicable to situations in that member names of a subclass hide members through the similar name in the superclass. Let consider this simple class hierarchy:
// using super to overcome name hiding.
class A {
int i;
}
//Create a subclass by extending class A.
class B extends A {
int i; // this i hides the i in A
B(int a, int b) {
super.i = a; // i in A
i = b; // i in B
}
void show( ) {
System .out.println("i in superclass: " = super.i);
System.out.println("i in subclass: " + i);
}
}
class UseSuper {
public static void main(String args[ ] ) {
B subOb = new B(1,2);
subOb.show( );
}
}
The program displays the following:
i in superclass: 1
i in subclass: 2
Although the instance variable i in B hides the i in A, super permit access for the i defined in the superclass. Super could also be used for call methods that are hidden by a subclass.