31. Super Keyword In Java


Instance Initializer block in Java: Previous                                  Next: Static Keyword in Java

Super Keyword In Java

super is a keyword in java which refers to the immediate super class object.

Use of super keyword in java: 

  1. 1. super can be used to call immediate super class constructor (constructor chaining).
  2. 2. super can be used to call immediate super class method on a subclass object from a subclass method.
  3. 3. super can be used to access immediate super class instance variable.

1. super can be used to call immediate super class constructor (constructor chaining).

Example:

SuperExample1.java
/**
 * This program is used to show the use of super
 * keyword to invoke super class constructor using
 * super explicitly.
 * @author java tutorial point
 */
class Display {
       Display(){
          System.out.println("Super class constructor called.");
       }
}
 
public class SuperExample1 extends Display {
       SuperExample1(){
            //super keyword will call super class constructor.
            super();
 
            System.out.println("Current class constructor called.");
        }
 
        public static void main(String args[]){
                SuperExample1 obj = new SuperExample1();
        }
}

Output:

Super class constructor called.
Current class constructor called.

If super is not used explicitly compiler will automatically add super as the first statement.

Example:

SuperExample2.java
/**
 * This program is used to show the use of super
 * keyword to invoke super class constructor using
 * super implicitly.
 * @author java tutorial point
 */
class Display {
      Display(){
               System.out.println("Super class constructor called.");
      }
}
 
public class SuperExample2 extends Display {
        SuperExample2(){
              //compiler automatically add super here.
              System.out.println("Current class constructor called.");
        }
 
        public static void main(String args[]){
               SuperExample2 obj = new SuperExample2();
        }
}

Output:

Super class constructor called.
Current class constructor called.

2. super can be used to call immediate super class method on a subclass object from a subclass method.

 If super class and subclass have same methods and that method is called from subclass method than subclass method is called.

Example:

SuperExample4.java
/**
 * This program is used to show that if super class and subclass
 * have same methods and that method is called from subclass
 * method than subclass method is called.
 * @author java tutorial point
 */
class Display {
      public void display(){
              System.out.println("display method of super class.");
      }
}
 
class Show extends Display {
       public void display(){
              System.out.println("display method of sub class.");
       }
 
       public void show(){
              System.out.println("show method of sub class.");
              //subclass display method is called.
              display();
      }
}
 
public class SuperExample4 {
 public static void main(String args[]){
  //create Show class object.
  Show obj = new Show();
  //method call
  obj.show();
 }
}

Output:

show method of sub class.
display method of sub class.
 Above problem can be solved with super keyword.

Example:

SuperExample3.java
/**
 * This program is used to show the use super keyword
 * to invoke the super class method from subclass method.
 * @author java tutorial point
 */
class Display {
 public void display(){
  System.out.println("display method of super class.");
 }
}
 
class Show extends Display {
 public void display(){
  System.out.println("display method of sub class.");
 }
 
 public void show(){
  System.out.println("show method of sub class.");
  //super class display method is called.
  super.display();
 }
}
 
public class SuperExample3 {
 public static void main(String args[]){
  //create Show class object.
  Show obj = new Show();
  //method call
  obj.show();
 }
}

Output:

show method of sub class.
display method of super class.

If super class and subclass not have same methods and method of super class is called from subclass method than super class method is called. There is  no need of super keyword.

Example:

SuperExample5.java
/**
 * This program is used to show that if super class and subclass
 * not have same methods and method of super class is called from  
 * subclass method than super class method is called.There is 
 * no need of super keyword.
 * @author java tutorial point
 */
class Display {
 public void display(){
  System.out.println("display method of super class.");
 }
}
 
class Show extends Display {
 
 public void show(){
  System.out.println("show method of sub class.");
  //no need of super keyword here.
  display();
 }
}
 
public class SuperExample5 {
 public static void main(String args[]){
  //create Show class object.
  Show obj = new Show();
  //method call
  obj.show();
 }
}

Output:

show method of sub class.
display method of super class.

3. super can be used to access immediate super class instance variable.

If super class and subclass have same instance variables and that variable is called from subclass than subclass instance variable will be referred.

Example:

SuperExample6.java
/**
 * This program is used to show that if super class and subclass
 * have same variable name and that variable is used in subclass 
 * method than subclass variable will be called.
 * @author java tutorial point
 */
class Display {
 int num = 100;
}
 
class Show extends Display {
 int num = 200;
 
 public void show(){
  //sub class instance variable will be referred.
  System.out.println("num = " + num);
 }
}
 
public class SuperExample6 {
 public static void main(String args[]){
  //create Show class object.
  Show obj = new Show();
  //method call
  obj.show();
 }
}

Output:

num = 200

 Above problem can be solved with super keyword.

Example:

SuperExample7.java
/**
 * This program is used to show the use super keyword to invoke 
 * the super class instance variable from subclass method.
 * @author javatutorialpoint
 */
class Display {
 int num = 100;
}
 
class Show extends Display {
 int num = 200;
 
 public void show(){
  //super class instance variable will be referred.
  System.out.println("num = " + super.num);
 }
}
 
public class SuperExample7 {
 public static void main(String args[]){
  //create Show class object.
  Show obj = new Show();
  //method call
  obj.show();
 }
}

Output:

num = 100

If sub class and super class instance variables are not same than there is no need of super keyword.

Example:

SuperExample8.java
/**
 * This program is used to show that if sub class and 
 * super class instance variables are not same than there
 * is no need of super keyword.
 * @author javatutorialpoint
 */
class Display {
 int num = 100;
}
 
class Show extends Display {
 
 public void show(){
  //super class instance variable will be referred.
  System.out.println("num = " + num);
 }
}
 
public class SuperExample8 {
 public static void main(String args[]){
  //create Show class object.
  Show obj = new Show();
  //method call
  obj.show();
 }
}

Output:

num = 100

Instance Initializer Block In Java: Previous                                  Next: Static Keyword In Java