Tuesday, 21 April 2015

super in Java

The super keyword in java is essentially a reference variable that can be used to refer to class' immediate parent class.

Usage of super

  • Invoke the constructor of the super class.
  • Accessing the variables and methods of parent class.

First let's see how code will look like if super() is not used.

Let's say there is a super class Shape with instance variables length and breadth. Another class Cuboids extends it and add another variable height to it. With these 2 classes and not using super(), constructors of the class will look like -

public class Shape {
 int length;
 int breadth;
 Shape(){
  
 }
 Shape(int length, int breadth){
  this.length = length;
  this.breadth = breadth;
 }
}

class Cuboids extends Shape{
 int height;
 Cuboids(int length, int breadth, int height){
  this.length = length;
  this.breadth = breadth;
  this.height = height;
 }
}

It can be noticed there are 2 problems with this approach -

  • Duplication of code as the same initialization code in the constructor is used twice.
  • Second and most important problem is super class instance variables should be given such an access modifier that they can be used in child class, thus violating the OOP principle of Encapsulation.

So super() comes to the rescue and it can be used by a child class to refer to its immediate super class.
Let's see how super() can be used.

Invoke the constructor of the super class

From the constructor of the subclass if constructor of the immediate super class has to be called to initialize the variables that are residing in super class super() can be used.
Note that in that case super() should be the first statement inside the subclass' constructor. This will ensure that if you call any methods on the parent class in your constructor, the parent class has already been set up correctly.

Example code using super

public class Shape {
 private int length;
 private int breadth;
 Shape(){
  
 }
 Shape(int length, int breadth){
  this.length = length;
  this.breadth = breadth;
 }
}

class Cuboids extends Shape{
 private int height;
 Cuboids(int length, int breadth, int height){
  // Calling super class constructor
  super(length, breadth);
  this.height = height;
 }
}

Here it can be noticed that the instance variables are private now and super() is used to initialize the variables residing in the super class thus avoiding duplication of code and ensuring proper encapsulation.

Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error.

That will happen because; If a constructor is explicitly defined for a class, then the Java compiler will not insert the default no-argument constructor into the class. You can see it by making a slight change in the above example.

In the above example, if I comment the default constructor and also comment the super statement then the code will look like -

public class Shape {
  private int length;
  private int breadth;
  /*Shape(){
   
  }*/
  Shape(int length, int breadth){
   this.length = length;
   this.breadth = breadth;
  }
}

class Cuboids extends Shape{
  private int height;
  Cuboids(int length, int breadth, int height){
   // Calling super class constructor
   /*super(length, breadth);*/
   this.height = height;
  }
}

This code will give compile-time error. "Implicit super constructor Shape() is undefined".

Accessing Superclass Members

If method in a child class overrides one of its superclass' methods, method of the super class can be invoked through the use of the keyword super. super can also be used to refer to a hidden field that is if there is a variable of the same name in the super class and the child class then super can be used to refer to the super class variable.

public class Car {
 int speed = 100;
 
}

class FastCar extends Car{
 int speed = 200;
 FastCar(int a , int b){
  super.speed = 100;
  speed = b;
 }
}

Here, in the constructor of the FastCar class super.speed isused to access the instance variable of the same name speed in the super class.

Points to note -

  • super keyword in java is a reference variable to refer to class' immediate parent class.
  • super can be used to invoke the constructor of the immediate parent class, that's help in avoiding duplication of code, also helps in preserving the encapsulation.
  • If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass.
  • super can also be used to access super class members.
  • If a variable in child class is shadowing a super class variable, super can be used to access super class variable.

That's all for this topic super in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. final in Java
  2. this in Java
  3. static in Java
  4. Core Java basics interview questions

You may also like -

>>>Go to Java Basics page

2 comments:

  1. Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error.

    The Sentence "If the super class does not have a no-argument constructor, you will get a compile-time error." is it correct ? . I tried and didn't got any compile time or run time exception.

    ReplyDelete
    Replies
    1. If a constructor is explicitly defined for a class, then the Java compiler will not insert the default no-argument constructor into the class. As you see in the example given, since Shape has a 2 arg constructor so default constructor won't be automatically inserted. In that example you can comment the no-arg constructor and comment the explicit super call then you should get an error.

      Delete