Using super keyword in inheritance

Super keyword

 

Whenever a subclass needs to refer to its immediate superclass, it can do so using the keyword super.

 

super keyword has two general uses :

  1. Calling the superclass constructor
  2. Accessing member of the superclass hidden by subclass

 

Calling superclass constructor using super

 

We need not access the default constructor of the superclass, as it is available to the subclass by default.

Here is an example demonstrating this :

 

public class ClassA {
  
  ClassA(){
    System.out.println("ClassA default constructor");
  }
}

public class ClassB extends ClassA{

  ClassB(){
    System.out.println("ClassB default constructor");
  }
}

public class InheritanceExample {

  public static void main(String[] args) {
    
    ClassB obj = new ClassB();
  }

}

Here, when the subclass object is created, first the superclass default constructor is called and then only the subclass constructor is called.

 

Running this will generate following output :

ClassA default constructor

ClassB default constructor

 

A subclass constructor can invoke its superclass constructor using the following syntax :

super(argument-list);

 

If super() call is present in the subclass’s constructor, it must always be the first statement executed.

 

Here is an example of the same :

 

public class ClassA {
  
  int a;
  
  ClassA(int a){
    System.out.println("ClassA constructor called.. initializing a");
    this.a = a;
  }
}

public class ClassB extends ClassA{

  int b;
  
  ClassB(int a, int b){
    super(a);
    System.out.println("ClassB constructor called.. initializing rest");
    this.b = b;
  }
  
  public void getProduct(){
    System.out.println("Product of " + a + " and " + b + " is " + a * b);
  }
}

public class InheritanceExample {

  public static void main(String[] args) {
    
    ClassB obj = new ClassB(5,10);
    obj.getProduct();
  }

}


 

Using super to overcome name hiding in subclass

 

This is applicable in situations where member names in subclass are same as member names in superclass and thus hide the same name of the superclass.

 

The syntax to access the superclass member (variable or method) is :

super.member

 

Here is an example of the same :

 

public class ClassA {
  
  int a=10;
  
  public void show(){
    System.out.println("Superclass method, a = " + a);
  }

}

public class ClassB extends ClassA{

  int a=25;
  
  public void show(){
    System.out.println("Subclass variable, a = " + a);
    
    //Accessing superclass variable
    System.out.println("Superclass variable, a = " + super.a);
    
    //Accessing superclass method
    super.show();
  }
}

public class InheritanceExample {

  public static void main(String[] args) {
    
    ClassB obj = new ClassB();
    obj.show();
  }

}

 

In the above example, superclass variable a is accessed in subclass as super.a

Similarly, superclass method show() is accessed in subclass as super.show()

 

 

© 2015, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags