Rajinder Menu

The super Keyword in Java



The super keyword is used to access a field, member or constructor of a superclass from within its subclass.


super and Fields

Consider following example

class Parent{

  String num="ten";
  int i=13;
}

public class Child extends Parent{
  
  int num=10;
  
  public void print() {
   System.out.println("The value of num is:"+num);

  }

  public static void main(String[] args) {
   Child childObject= new Child();
   childObject.print();
  }
}

Output:

The value of num is:10

Here in above code the field num in subclass is hiding the field num in superclass(note that despite that their data types are different).

Now subclass Child has two nums-one is int and one is String(inherited from superclass).

In print() method we have printed the num of subclass. But how to access superclass num in subclass Child?

Here the super keyword comes into play. With the super keyword we can access the superclass variable from within the subclass. For this change the print method in subclass Child as:

public void print() {

  //newly added line
  System.out.println("The value of super.num is:"+super.num);
  System.out.println("The value of num is:"+num);

}

num in above method is representing num of subclass while super.num is representing num of superclass.

If you run above program with changed print() method then output will be:

Output:

The value of super.num is:ten
The value of num is:13

Note that super can access any variable in superclass from within the subclass(except static variables...coming in later section). It is not necessary that superclass variable is a hidden variable like num.

For example to access variable i from subclass change the print() method as:

public void print() {
  //newly added line
  System.out.println("The value of super.num is:"+super.num);
  System.out.println("The value of super.num is:"+super.i);
  System.out.println("The value of num is:"+num);
}

Output:

The value of super.num is:ten
The value of super.i is:13
The value of num is:10


super and Methods

super keyword can also be used for accessing superclass method from within the subclass.

Consider following example

class Parent{

  public void print() {
    System.out.println("This method is in Parent class");
  }

  public void print1() {
    System.out.println("This is another method in Parent class");
  }
}

public class Child extends Parent{

  public void print() {
    System.out.println("This method is in Child class");
  }

  public static void main(String[] args) {

    Child childObject= new Child();
    childObject.print();
  }
}

Output:

This method is in Child class

Now to access print() and print1() method from subclass Child change the print method() in subclass as:

public void print() {

  super.print(); //newly added line
  super.print1(); //newly added line
  System.out.println("This method is in Child class");
}

Output:

This method is in Parent class
This is another method in Parent class
This method is in Child class


super cannot be used in static context

super cannot be called from a static method. For above example you cannot use super keyword in static method main() as:

public static void main(String[] args) {
  
  Child childObject= new Child();
  childObject.print();
  super.print(); //will give error: cannot use super in static context

}

But super can be used to call the static method. For example

class Parent{

  public void print() {
    System.out.println("This method is in Parent class");
  }

  //static method
  public static void print1() {
    System.out.println("This is another method in Parent class");
  }

}


public class Child extends Parent{

  public void print() {
  
    super.print();
    super.print1(); //calling static method
    System.out.println("This method is in Child class");
  }

  public static void main(String[] args) {

    Child childObject= new Child();
    childObject.print();
  }
}

Output:

This method is in Parent class
This is another method in Parent class
This method is in Child class

Similarly super can be used to access static member field also.


super and constructors

We know that constructors[Link] are not members of a class so they are not inherited by subclasses but the constructor of a superclass can be invoked from within the subclass by using super keyword.

For example

class Parent{

  int num;

  //superclass no-arg constructor
  Parent(){
    num=10;
  }
}

public class Child extends Parent{
  
  int i;

  Child(int i){
    super(); //calling superclass no arg constructor
    this.i=i;
  }

  public void print() {
    System.out.println("The value of num is:"+num);
    System.out.println("The value of i is:"+i);
  }

  public static void main(String[] args) {
    
    Child childObject= new Child(13);
    childObject.print();
  }
}

Output:

The value of num is:10
The value of i is:13

In above code super() is used to call the superclass no-argument constructor. This is also known as explicitly calling superclass constructor.

Now consider following example for parametrized superclass constructor[Link]:

class Parent{
  
  int num;
  
  //superclass parameterized constructor
  Parent(int num){
    this.num=num;
  }
}

public class Child extends Parent{

  int i;

  Child(int i,int num){
    super(num); //calling superclass parameterized constructor
    this.i=i;
  }

  public void print() {

    System.out.println("The value of num is:"+num);
    System.out.println("The value of i is:"+i);
  }

  public static void main(String[] args) {

    Child childObject= new Child(13,10);
    childObject.print();
  }
}

Output:

The value of num is:10
The value of i is:13

Here in above code we have used super(num) to call the parameterized superclass constructor

Parent(int num){
 ...
}

I would like to know your comments and if you liked the article then please share it on social networking buttons.



No comments:

Post a Comment