March 20, 2024

super in Java With Examples

Super keyword in Java is used to refer to the immediate parent class of the sub class. You can use super keyword in Java in following ways-

  • You can invoke the constructor of the parent class, doing that using super makes your code more compact and classes are better encapsulated. See example.
  • You can also use super keyword to access the field or method of the parent class that has been hidden by the child class. See example.

Using super to invoke constructor of super class

To see how super keyword in Java can be used to invoke constructor of the immediate parent class and how it helps in having better encapsulation and in reducing duplication of code, first let’s see a code where super is not used.

class A {
  int x;
  int y;
  public A(){
    
  }
  A(int x, int y){
    this.x = x;
    this.y = y;		
  }
}

public class B extends A{
  int z;
  public B(int x, int y, int z) {
    // x and y from parent class
    this.x = x;
    this.y = y;
    this.z = z;
  }
  public static void main(String[] args) {
    B obj = new B(5, 6, 7);
    System.out.println("x=" + obj.x + " y= "+ obj.y + " z= "+ obj.z);
  }
}
Output
x=5 y= 6 z= 7

In the code, class B extends class A. If you notice, the constructor of class B again initializes the fields it inherits from class A (x and y). Same initialization is also done in constructor of class A leading to duplication of code.

Another problem here is the fields in class A can’t be marked as private as the fields have to be accessed in the child class B that means class A is not properly encapsulated.

Using super keyword

Let’s see the same example again when super keyword is used to invoke the constructor of the parent class.

class A {
  private int x;
  private int y;
  public A(){
    
  }
  A(int x, int y){
    this.x = x;
    this.y = y;		
  }
}

public class B extends A{
  int z;
  public B(int x, int y, int z) {
    //invoking parent class constructor
    super(x, y);
    this.z = z;
  }
  public static void main(String[] args) {

  }
}

In the code you can see that the super is used to invoke the constructor of the parent class which initializes the fields in parent class. That way, x and y fields can be marked as private so class A has better encapsulation. Also the same initialization is not needed in constructor of class B so no duplication of code.

Note that call to super() must be the first statement in the subclass constructor otherwise there will be a compilation error "Constructor call must be the first statement in a constructor".

Using super to access the field and method of the parent class

Another use of super keyword in Java is to access the field or method of the parent class. In child class if you have a field or method having the same name as in the parent class then the child class member overrides the parent class member of the same name. In that case if you want to access the member of the parent class then you can use super keyword.

Example code

In the example there is a display() method in class A and again display() method in the child class B which overrides the method of the parent class. In order to display the fields of class A if you want to invoke the display method of class A, you can do it using the super keyword.

class A {
  private int x;
  private int y;
  public A(){
    
  }
  A(int x, int y){
    this.x = x;
    this.y = y;		
  }
  public void display() {
    System.out.println("In display method of super class");
    System.out.println("x= " + x + " y= " + y);
  }
}

public class B extends A{
  int z;
  public B(int x, int y, int z) {
    //invoking parent class constructor
    super(x, y);
    this.z = z;
  }
  public void display() {
    // invoking display method of parent class
    super.display();
    System.out.println("In display method of child class");
    System.out.println("z= " + z);
  }
  public static void main(String[] args) {
    B obj = new B(5, 6, 7);
    obj.display();		
  }
}
Output
In display method of super class
x= 5 y= 6
In display method of child class
z= 7

That's all for the topic super in Java With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment