Super keyword in java - Invoke constructor, Access instance variable, Invoke instance method of immediate super class using super


Usage of super keyword in java -
  1. Invoke constructor of immediate super class- super keyword can be used to invoke constructor of immediate super class.

  1. Access instance variable/ static variable of immediate super class- super keyword can be used to access instance variable of immediate super class.

  1. Invoke instance method of immediate super class- super keyword can be used to invoke constructor of immediate super class.


1) Invoke constructor of immediate super class- super keyword can be used to invoke constructor of immediate super class.

Implicitly first statement of constructor is super(), [that means by default first statement of constructor super() is called, super() calls implicit/explicit no-arg constructor of superclass].


Let’s we have superclass and subclass like this -
class SuperClass{
}
class SubClass extends SuperClass{
}

compiler will add following implicit code>
default implicit no-arg constructor and
super()
class SuperClass{
   SuperClass(){ //no-arg /no argument constructor
          super();
}
}
class SubClass extends SuperClass{
   SubClass(){
          super();
   }
}


Program - Let’s write these implicit statements explicitly to understand flow -
class SuperClass{
   SuperClass(){
        super(); //will call constructor of java.lang.Object(bcz all classes extend Object)
        System.out.println("constructor of SuperClass");
   }
}
class SubClass extends SuperClass{
   SubClass(){
         super();  //will call constructor of SuperClass
         System.out.println("constructor of SubClass");
   }
  
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class MyClass{
   public static void main(String[] args) {
          new SubClass(); //will call constructor of SubClass
   }
}
/* OUTPUT
constructor of SuperClass
constructor of SubClass
*/



  • First line in constructor can either be super() or this(). But, super() and this() cannot be used in same constructor. (Please ensure that super() or this() whichever is used must be be first line of constructor, else you will face compilation error)
We read in above points that compiler implicitly adds super() as first line of constructor, but if we add this() explicitly than compiler doesn’t add super()

Program to understand usage of this() and super() in constructor and also to show this() and super() cannot be used in same constructor >
class SuperClass{
   SuperClass(){
         System.out.println("no-arg constructor of SuperClass");
   }
}
class SubClass extends SuperClass{
   SubClass(){
          this(1);
          System.out.println("no-arg constructor of SubClass");
   }
   SubClass(int i){
          super();
          System.out.println("int-arg constructor of SubClass");
   }
  
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class MyClass{
   public static void main(String[] args) {
      new SubClass(); //will call constructor of SubClass
   }
}
/* OUTPUT
no-arg constructor of SuperClass
int-arg constructor of SubClass
no-arg constructor of SubClass
*/




2) Access instance variable/ static variable of immediate super class- super keyword can be used to access instance variable of immediate super class.

It’s important to know that instance or static variable are never overridden in java.
Call to instance or static variables is bonded at compile time using compile time data (i.e. based on type of reference variable type) irrespective of object assigned to it.

Example Program>
class SuperClass{
   int i=2; //instance variable
}
class SubClass extends SuperClass{
   int i=4; // //instance variable
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com
* Main class */
public class SuperTest {
   public static void main(String[] args) throws Exception {
         
          SuperClass obj1=new SubClass(); //Note : Reference variable is of
                                            //SuperClass type
          System.out.println("SuperClass > "+ obj1.i); //Will print 2 because of
                                                         //SuperClass type reference,
                                           //Call to instance variables is bonded at compile time
                                           //using compile time data (i.e. SuperClass)
                                           //irrespective of SubClass object assigned to it.
         
          SubClass obj2=new SubClass(); //Note : Reference variable is of
                                         //SubClass type
          System.out.println("SubClass   > "+ obj2.i); //Will print 4 because of
                                                      //SubClass type reference.
         
   }
}
/*OUTPUT
SuperClass > 2
SubClass   > 4
*/



Program > Now, Let’s create program to access instance variable of immediate super class using super keyword.
class SuperClass{
   int i=2; //instance variable
}
class SubClass extends SuperClass{
   int i=4; // //instance variable
  
   void method(){
          System.out.println("SubClass   i = "+i);            //4
         
          //But now, How to access i of superClass? use super keyword.
          System.out.println("SuperClass i = "+ super.i);     //2
   }
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com
* Main class */
public class SuperTest {
   public static void main(String[] args) throws Exception {
          SubClass obj=new SubClass(); //Note : Reference variable is of SubClass type
          obj.method(); //will call method() of SubClass
   }
}
/*OUTPUT
SubClass   i = 4
SuperClass i = 2
*/

In the above program we accessed instance variable (i) of superClass in SubClass using super keyword. Likewise, we can also access static variables of superClass using super keyword.
Note : We need to use super keyword to access variables of SuperClass in SubClass only when variable name is same in both SuperClass and SubClass. If variable name is different in SuperClass and SubClass than SuperClass variables are inherited in SubClass and they can be accessed directly in SubClass without using super keyword.


It’s important to know super keyword can’t be used outside the SubClass to access to access instance or static variable of SuperClass using object of SubClass, any such attempt will cause compilation error.
Example >



3) Invoke instance method of immediate super class- super keyword can be used to invoke instance method of immediate super class.
class SuperClass{
  
   //instance method
   void method(){
          System.out.println("superClass method");
   }
}
class SubClass extends SuperClass{
   //instance method
   @Override
   void method(){
          System.out.println("SubClass method");
          super.method();   //will call method() of SuperClass.
   }
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com
* Main class */
public class SuperTest {
   public static void main(String[] args) throws Exception {
          SuperClass obj=new SubClass();
          obj.method();  //will call overridden method() of SubClass
   }
}
/*OUTPUT
SubClass   i = 4
SuperClass i = 2
*/
In the above program we accessed instance method of superClass in SubClass using super keyword.
Note : We need to use super keyword to access instance method of SuperClass in SubClass only when instance method name is same in both SuperClass and SubClass (i.e. SubClass overrides superClass instance method). If instance method name is different in SuperClass and SubClass than SuperClass instance methods are inherited in SubClass and they can be accessed directly in SubClass without using super keyword.

We need not to use super keyword for accessing static methods of superClass in SubClass because static method name cannot be same in both SuperClass and SubClass (i.e. static methods are not overridden in java)




RELATED LINKS>


eEdit
Must read for you :