CodesJava

Easy learning with example program codes

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

Related Topics:

Package in java with example.
Access modifier in java with example.
Static import in java with example.
Package class in java with example.
this in java with example.
Instance initialize/ Anonymous block in java with example.
super in java with example.
Static in java with example.
final in java with example.

 

Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
JVM, JRE and JDK.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap