super Keyword in Java

Constructors and super

In Java, the super keyword is used to refer to the closest parent of a class that it inherited from.

public class SuperClass {
   public SuperClass(int x) {
      // Constructor code goes here
   }
}

public class SubClass extends SuperClass {
   public SubClass() {
      super(10); // Calls the superclass constructor with the int argument 10
   }
}

No-arg constructors and super

Base class must always call the parent class constructor. The only exception is that if the super class has a no-argument constructor, then qw do not need to call super() in the subclass’s constructor. This is because the default behavior is to automatically call the no-arg constructor of the superclass.

public class SuperClass {
   public SuperClass() {
      // Constructor code goes here
   }
}

public class SubClass extends SuperClass {
   public SubClass() {
      super(); // This is unnecessary because Superclass has no no-argument
               // constructor which is automatically called. 
   }
}

In the example above, we can remove super() because the SuperClass has a no argument constructor SuperClass() so we don’t need to call it explicitly. Java compiler will automatically add a call to the no-arg constructor of the superclass.

public class SuperClass {
   protected int x;
   public SuperClass() {
      x = 10;
   }
}

public class SubClass extends SuperClass {
   private int y;
   public SubClass() {
      y = 20; //super.x is 20
   }
}

Here,the SubClass constructor does not explicitly call a superclass constructor. However, since the SuperClass has a no-arg constructor, Java will automatically insert a call to super() at the beginning of the SubClass constructor. The bytecode for the SubClass constructor would be equivalent to the following:

public SubClass() {
   super();
   y = 20;
}

Using super to access Superclass variables

We can also use super to access a member of the superclass from a subclass:

public class SuperClass {
   protected int x;
   public SuperClass() {
      x = 10;
   }
}

public class SubClass extends SuperClass {
   public void printX() {
      System.out.println(super.x); // Prints 10
   }
}

Using super to access overridden methods

Another common use case for super is to call a method of the superclass that has been overridden in the subclass:

public class SuperClass {
   public void printMessage() {
      System.out.println("Message from the superclass");
   }
}

public class SubClass extends SuperClass {
   @Override
   public void printMessage() {
      super.printMessage(); //call the method in the superclass
      System.out.println("Message from the subclass");
   }
}

super vs this

It is related to this keyword which refers to the current class.

  • super keyword is used to refer to the superclass of a class
  • this keyword is used to refer to the current instance/object of a class
class SuperClass {
   protected int x;
   public SuperClass(int x) {
      this.x = x;
   }
}

public class SubClass extends SuperClass {
   private int x;
   public SubClass(int n) {
      super(n * 2); // Calls the superclass constructor and passes the argument * 2
      this.x = n; // Assigns the value of the argument to classes's x variable
   }

   public void print() {
      System.out.println("Superclass x is " + super.x); 
      System.out.println("This class x is " + this.x);
   }

   public static void main(String ...args) {
    SubClass s = new SubClass(10);
    s.print();  // Output: Superclass x is 20
                // This class x is 10
   }
}

In this example, the SubClass constructor takes an integer argument. It calls the super class constructor using with 2 times of the value of it’s argument e.g. super(n*2). The SuperClass constructor assign this value to its class variable x. Next, it assigns the value of its argument to its own class variable which is also called x. The print() function prints the value for both x’s in Superclass and SubClass and we can see that super class’s x is 2x the value of subclass’s x.

Speak Your Mind