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. super can be used to call immediate super class constructor (constructor chaining).
  2. super can be used to call immediate super class method on a subclass object from a subclass method.
  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 W3spoint
 */
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.

Download this example.

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 W3spoint
 */
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.

Download this example.

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 W3spoint
 */
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.

Download this example.
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 W3spoint
 */
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.

Download this example.

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 W3spoint
 */
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.

Download this example.

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 W3spoint
 */
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

Download this example.
 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 W3spoint
 */
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

Download this example.

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 W3spoint
 */
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

Download this example.

Difference between this and super keyword in java.

  1. this is used for accessing variables and methods of current class. super is used for accessing variables and methods of immediate super class.
  2. this is used in constructor chaining in current class. super is used in constructor chaining in immediate super class.

 
Next Topic: Static in java with example.
Previous Topic: Instance initialize/ Anonymous block in java with example.

 

Content Protection by DMCA.com
Please Share