Title: Use super to call superclass constructors and members.
Course: Java Programming (Oracle) (10055) > Section: Using Inheritance

Use allows the constructor to utilize the prewritten code from a parent class. In this way, the parent constructor is allowed to do its job and then the child constructor can add additional functionality to it. Super must be the first line in the constructor, if it is used. The following class is a child class of a class called SuperClassName. Coding for the constructor follows:

class LittleClass extends BigClass{

int a, b, c;

LittleClass(){

a = 6;
b = 7;
c = 8;

}

//body of class
}//end class definition

Use should include the uses of the super keyword:

- If a class overrides a method in a superclass, the super keyword can be used to bypass the overridden version in the class and access the version in the superclass.
- If a local variable in a method or a member variable in a class hides a member variable in the superclass (with the same name), the super keyword can be used to access the member variable in the superclass.
- Super can also be used in a constructor of the class to invoke an overloaded constructor in the superclass.

Sample code for accessing superclass members follows:

class BigSuperClass{
int info;
/*explicit coding of the default no argument constructor is required if another overridden constructor is created */
public BigSuperClass(){
}
/*An overloaded constructor requiring an integer parameter. Note that the signature is changed in order to cause
the overloading. */
public BigSuperClass(int val){
System.out.println(
"Inside the BigSuperClass constructor. ");
System.out.println(
"Setting BigSuperClass instance "
+ "variable info to " + val);
info = val;
System.out.println();//blank line
}
//end BigSuperClass constructor
}
//end BigSuperClass class definition
//===================================//
class SuperThree extends BigSuperClass{
/*Instance variable in subclass has same
name as instance variable in BigSuperClass*/
int info;
//Subclass no arg constructor
public SuperThree(){
Duty/Concept Area: Using Inheritance
91
//call the constructor requiring parameters from BigSuperClass
super(700);
System.out.println(
"In subclass constructor.");
System.out.println(
"Setting subclass instance variable info "
+ "to 56");
info = 56;
System.out.println();//blank line
}//end subclass constructor
//---------------------------------//

/*The following method illustrates use of this and super and clarifies the identification of the local variable called info, instance variable of subclass called info, and instance variable of BigSuperClass called info. All three variables have the same name.

*/
void confusingVariableNames(){
int info = 75
//local variable
System.out.println(
"In method confusingVariableNames");
System.out.println("Local variable info = "
+ info);
System.out.println(
"Subclass instance variable info = "
+ this.info);
System.out.println(
"BigSuperClass instance variable info = "
+ super.info);
}
//end method confusingVariableNames
//---------------------------------//
public static void main(String[] args){
SuperThree objOfSubClass = new SuperThree();
System.out.println("Executing the main method");
System.out.println(
"Subclass instance variable info = "
+ objOfSubClass.info);
System.out.println();//blank line
objOfSubClass.confusingVariableNames();
}
//end main method
}
//End SuperThree class definition.