Super Keyword in Java | Uses of Java Super Keyword with Example

In the previous tutorial, we have learned this keyword in java. In this tutorial, we will learn the Super keyword in java. The Super Keyword in Java is a reference variable that is used to refer immediate parent class objects. The Super keyword must be used with the concept of inheritance therefore before learning the super keyword in java you must have the knowledge of inheritance in java.


3 Uses of Super keyword in Java:

1. The super keyword can be used to refer immediate parent class objects.
2. The super keyword can be used to invoke the immediate parent class method.
3. The super keyword can be used to invoke the immediate parent class constructor.


1. Example of super keyword to refer immediate parent class object.

When we have a variable in the child class that is already present in the parent class then we can use the super keyword to access the variable of the parent class in the child class. Let's understand with an example.

class Parent{
	int num = 10;  //instance variable.
}
class Child extends Parent{
	int num = 20;
	void display(){
		//print value of child class.
		System.out.println("Value of num variable in child class: " +num);
		//print value of parent class.
		System.out.println("Value of num variable in parent class: " +super.num);
	}	
}
class Main{
	public static void main(String args[]){
		Child obj = new Child();
		obj.display();
	}
}


Output:

Value of num variable in child class: 20
Value of num variable in parent class: 10




2. Example of the super keyword to invoke parent class method:

When we have a method in child class that is already present in the parent class then we can use the super keyword to access the method of the parent class in the child class. Let's understand with an example.

class Parent{
	void display(){
		System.out.println("This is parent class method.");
	}
}
class Child extends Parent{
	void display(){
		System.out.println("This is child class method.");
	}
	void showMsg(){
		display();
		super.display();
	}
}
class Main{
	public static void main(String args[]){
		Child obj = new Child();
		obj.showMsg();
	}
}


Output:

This is child class method.
This is parent class method.



3. Example of the super keyword to invoke parent class constructor:

The super keyword in java can also be used to call parent class constructor. The invocation of parent class constructor must be the first line in the child class constructor. Let's understand this with an example.

class Parent{
	Parent(){
		System.out.println("Parent class constructor.");
	}
}
class Child extends Parent{
	Child(){
		//call parent class constructor.
		super();
		System.out.println("Child class construtor.");
	}
}
class Main{
	public static void main(String args[]){
		Child obj = new Child();
	}
}


Output:

Parent class constructor.
Child class constructor.



You may also like these posts:

1. Basic data types in java with example.
Super Keyword in Java | Uses of Java Super Keyword with Example Super Keyword in Java | Uses of Java Super Keyword with Example Reviewed by Prashant Srivastava on December 24, 2019 Rating: 5

No comments:

Powered by Blogger.