super keyword in java

In the previous session, we have seen the usage of method overriding. In this session, we will see how to use the keyword super in java programming.

The super keyword is used to refer immediate parent class members.

Objective of super keyword:

There are following objectives of using super keyword.

  1. Accessing super class or say parent class instance variables
  2. Calling methods in super class or say parent class
  3. Calling constructor in super class or say parent class

Let’s discuss them in details with examples.

1. Accessing super class or say parent class instance variables

The super keyword can be used to access variables present in its immediate parent class.

First understand the problem if super keyword is not used.

Consider, we have 2 classes where MobileStore is the parent class and myMobile is the child class. There is a common variable called mobile  in both the classes. We want to print parent’s class variable. But in code it prints variable from child class , even if child class is extending the parent class.

//Parent or superclass
 class MobileStore {

	String mobile = "Samsung";
 }

 //Child class
  public class myMobile extends MobileStore{
	String mobile = "iPhone";
	
	void showMobile(){
		System.out.println("Mobile is "+mobile);  // It will print the iPhone which is child class 
	}
	public static void main(String[] args) {
		
		myMobile mob = new myMobile();
		mob.showMobile();
	}	
}
Output : Mobile is iPhone

Now, let’s see how this issue will be resolved by using the super keyword. We have refer the superclass variable bu using super keyword.

//Parent or superclass
 class MobileStore {

	String mobile = "Samsung";
 }

 //Child class+
  public class myMobile extends MobileStore{
	String mobile = "iPhone";
	
	void showMobile(){
		System.out.println("Mobile is "+super.mobile);  // It will print the variable from superclass
	}
	public static void main(String[] args) {
		
		myMobile mob = new myMobile();
		mob.showMobile();
	}	
}
Output : Mobile is Samsung

 


2. Calling methods in super class or say parent class:

We can call the method of parent class by using super  keyword as shown in example.

//Parent or superclass
 class MobileStore {	
	void mobilePrice(){    
		System.out.println("In parent class : 10000.00");
	}
 }

 //Child class
  public class myMobile extends MobileStore{
	
	void mobilePrice(){     // same method in child 
		System.out.println("IN child class : 15000.00");
	}	
	
	void showMobile(){
		mobilePrice();    // this will invoke the child method
		super.mobilePrice();   // this will invoke the superclass method
	}
	
	public static void main(String[] args) {	
		myMobile obj = new myMobile();
		obj.showMobile();		
	}	
}
Output : IN child class : 15000.00
In parent class : 10000.00

 

3. Calling constructor in super class or say parent class:

The super keyword can also be used to invoke the parent class constructor as shown below.

//Parent or superclass
 class MobileStore {
	 
	 MobileStore(){    
		System.out.println("I am in parent class.");
	}
 }

 //Child class
  public class myMobile extends MobileStore{
	
	  myMobile(){
		  super();       // This will invoke the parent class constructor
		  System.out.println("I am in child class");
	  }
	
	public static void main(String[] args) {	
		myMobile obj = new myMobile();		
	}	
}
Note: If super keyword is not used to call constructor, compiler adds super keyword automatically while calling constructor.

We have commented the super keyword in the below code, even though we get the same output as above.

//Parent or superclass
 class MobileStore {
	 
	 MobileStore(){    
		System.out.println("I am in parent class.");
	}
 }

 //Child class
  public class myMobile extends MobileStore{
	
	  myMobile(){
		  //super();       // This will invoke the parent class constructor
		  System.out.println("I am in child class");
	  }
	
	public static void main(String[] args) {	
		myMobile obj = new myMobile();		
	}	
}
Output:
I am in parent class.
I am in child class

When is it possible to invoke the superclass method without using super keyword?

It is possible when, child class does not have the same method as present in parent class. Then, we can call the method directly in child class without using the super keyword.

//Parent or superclass
 class MobileStore {	
	void getMobile(){    
		System.out.println("This is parent class.");
	}
 }

 //Child class
  public class myMobile extends MobileStore{

	void showMobile(){
		getMobile();    // this will invoke the parent method without using super keyword
	}
	
	public static void main(String[] args) {	
		myMobile obj = new myMobile();
		obj.showMobile();		
	}	
}

What if we don’t extend the parent class and even though we use super() keyword?

This will not throw any error but a default constructor of Object class will be called. Every class extends Object class by default which is a parent class for all the classes.

  public class myMobile{

	myMobile(){
		super();    // this will invoke the Object class budefault constructor
	}
	
	public static void main(String[] args) {	
		myMobile obj = new myMobile();		
	}	
}
Ask Question
If you have any question, you can go to menu ‘Features -> Q&A forum-> Ask Question’.Select the desired category and post your question.
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...