Super Keyword in Java

The super is a keyword defined in the java programming language. Keywords are basically reserved words which have specific meaning relevant to a compiler in java programming language.

Usage of java super Keyword

1.super is used to refer immediate parent class instance variable.

2.super() is used to invoke immediate parent class constructor.

3.super is used to invoke immediate parent class method.

1) super is used to refer immediate parent class instance variable.

Problem without super keyword

class Vehicle{  
  int speed=50;  
}  
class Bike3 extends Vehicle{  
  int speed=100;  
  void display(){  
   System.out.println(speed);//will print speed of Bike   
  }  
  public static void main(String args[]){  
   Bike3 b=new Bike3();  
   b.display();  
}  
}  
	
	
	Output:100
	
	

In the above example Vehicle and Bike both class have a common property speed. Instance variable of current class is refered by instance bydefault, but I have to refer parent class instance variable that is why we use super keyword to distinguish between parent class instance variable and current class instance variable.

Solution by super keyword

class Vehicle{  
  int speed=50;  
}  
  
class Bike4 extends Vehicle{  
  int speed=100;  
      
  void display(){  
   System.out.println(super.speed);   
  }  
  public static void main(String args[]){  
   Bike4 b=new Bike4();  
   b.display();  
     
}  
}  
 
Output:50

In the above example Vehicle and Bike both class have a common property speed. Instance variable of current class is refered by instance bydefault, but I have to refer parent class instance variable that is why we use super keyword to distinguish between parent class instance variable and current class instance variable.

Solution by super keyword

class Vehicle {

	int speed = 50;
}

class Bike4 extends Vehicle {
	int speed = 100;

	void display() {
		System.out.println(super.speed);// will print speed of Vehicle now
	}

	public static void main(String args[]) {
		Bike4 b = new Bike4();
		b.display();
	}
}
 

3 super can be used to invoke parent class method

The super keyword can also be used to invoke parent class method. It should be used in case subclass contains the same method as parent class as in the example given below:

 class Person {
	void message() {
		System.out.println("welcome");
	}
}

class Student16 extends Person {
	void message() {
		System.out.println("welcome to java");
	}

	void display() {
		message();// will invoke current class message() method
		super.message();// will invoke parent class message() method
	}

	public static void main(String args[]) {
		Student16 s = new Student16();
		s.display();
	}
}

In the above example Student and Person both classes have message() method if we call message() method from Student class, it will call the message() method of Student class not of Person class because priority is given to local.

In case there is no method in subclass as parent, there is no need to use super. In the example given below message() method is invoked from Student class but Student class does not have message() method, so you can directly call message() method.

Program in case super is not required

class Person{  
void message(){System.out.println("welcome");}  
}  
  
class Student1 extends Person{  
  
void display(){  
message();//will invoke parent class message() method  
}  
  
public static void main(String args[]){  
Student1 s=new Student1();  
s.display();  
}  
}