9. what is the use of super keyword

9. what is the use of super keyword

The super is a reference variable that is used to refer immediate parent class object.

Whenever you create the instance of subclass, an instance of parent class is created implicitly i.e. referred by super reference variable.

Super is a keyword which is used to access the method or member variables from the superclass. If a method hides one of the member variables in its superclass, the method can refer to the hidden variable through the use of the super keyword.

In the same way, if a method overrides one of the methods in its superclass, the method can invoke the overridden method through the use of the super keyword.


Note:

You can only go back one level.

In the constructor, if you use super(), it must be the very first code, and you cannot access any this.
 

Use of super Keyword:-

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

b) super() is used to invoke immediate parent class constructor

c) super is used to invoke immediate parent class method.

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


Problem create without super keyword:-

 

class car

{

int speed=250;

}

class Bike extends car{

int speed=150;

void display(){

System.out.println(speed);     //will print speed of Bike

}

public static void main(String args[]){

Bike b=new Bike();

b.display();

}

}

output :- 150

In the above example car 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:-


//example of super keyword :-

class car{

int speed=150;

}

class Bike extends car{

int speed=50;

void display(){

System.out.println(super.speed);//will print speed of car  now

}

public static void main(String args[]){

Bike b=new Bike();

b.display();

}

}

output:- 150

b) super is used to invoke parent class constructor:-

The super keyword can also be used to invoke the parent class constructor as given below:

class car{

car(){System.out.println("car is created");}

}

class Bike extends car{

Bike(){

super();//will invoke parent class constructor

System.out.println("Bike is created");

}

public static void main(String args[]){

Bike b=new Bike();

}

}

output:-car  is created
            Bike is create

note:- super() is added in each class constructor automatically by compiler.

As we know well that default constructor is provided by compiler automatically but it also adds super() for the first statement.If you are creating your own constructor and you don't have either this() or super() as the first statement, compiler will provide super() as the first statement of the constructor.
 

c) 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 ram{

void message()

{

System.out.println(" your welcome");}

}

class Student extends ram{

void message(){System.out.println(" your 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[]){

Student s=new Student();

s.display();

}

}

output:- your welcome to java
         your welcome

In the above example Student and ram both classes have message() method if we call message() method from Student class, it will call the message() method of Student class not of ram 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 when super is not required:-

class rock{

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

}

class Student extends rock{

void display(){

message();//will invoke parent class message() method

}

public static void main(String args[]){

Student s=new Student();

s.display();

}

}

output:- welcome


  

Read More →