All for Joomla All for Webmasters

super keyword in java

The super keyword in java 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 keyword makeforsure
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

  1. class Vehicle{
  2.   int speed=50;
  3. }
  4. class Bike3 extends Vehicle{
  5.   int speed=100;
  6.   void display(){
  7.    System.out.println(speed);//will print speed of Bike 
  8.   }
  9.   public static void main(String args[]){
  10.    Bike3 b=new Bike3();
  11.    b.display();
  12. }
  13. }

 

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

  1. //example of super keyword
  2. class Vehicle{
  3.   int speed=50;
  4. }
  5. class Bike4 extends Vehicle{
  6.   int speed=100;
  7.   void display(){
  8.    System.out.println(super.speed);//will print speed of Vehicle now
  9.   }
  10.   public static void main(String args[]){
  11.    Bike4 b=new Bike4();
  12.    b.display();
  13. }
  14. }

 

Output:50

2) super is used to invoke parent class constructor.

The super keyword can also be used to invoke the parent class constructor as given below:
  1. class Vehicle{
  2.   Vehicle(){System.out.println(“Vehicle is created”);}
  3. }
  4. class Bike5 extends Vehicle{
  5.   Bike5(){
  6.    super();//will invoke parent class constructor
  7.    System.out.println(“Bike is created”);
  8.   }
  9.   public static void main(String args[]){
  10.    Bike5 b=new Bike5();
  11. }
  12. }

 

Output:Vehicle is created
       Bike is created

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.

Another example of super keyword where super() is provided by the compiler implicitly.

  1. class Vehicle{
  2.   Vehicle(){System.out.println(“Vehicle is created”);}
  3. }
  4. class Bike6 extends Vehicle{
  5.   int speed;
  6.   Bike6(int speed){
  7.     this.speed=speed;
  8.     System.out.println(speed);
  9.   }
  10.   public static void main(String args[]){
  11.    Bike6 b=new Bike6(10);
  12.  }
  13. }

 

Output:Vehicle is created
       10

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:
  1. class Person{
  2. void message(){System.out.println(“welcome”);}
  3. }
  4. class Student16 extends Person{
  5. void message(){System.out.println(“welcome to java”);}
  6. void display(){
  7. message();//will invoke current class message() method
  8. super.message();//will invoke parent class message() method
  9. }
  10. public static void main(String args[]){
  11. Student16 s=new Student16();
  12. s.display();
  13. }
  14. }

 

Output:welcome to java
       welcome
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

  1. class Person{
  2. void message(){System.out.println(“welcome”);}
  3. }
  4. class Student17 extends Person{
  5. void display(){
  6. message();//will invoke parent class message() method
  7. }
  8. public static void main(String args[]){
  9. Student17 s=new Student17();
  10. s.display();
  11. }
  12. }

 

Output:welcome

 

PinIt
submit to reddit

Leave a Reply

Top