Java super keyword complete tutorial

  • Post author:
  • Post category:Java

In this section, we will learn what the super keyword is and how to use it in Java.

What is super keyword in Java?

The `super` keyword is used to call a method of the parent class in a child class. By default, without the use of `super` keyword, we can call a method in the parent class only if the child class does not have that method already!

For example, if in the parent class there’s a method named `sum()` but the child class has the same method as well, without the use of `super` keyword, the compiler will pick up the `sum()` method in the child class! But if we prefix this method with the keyword `super`, it is a signal to the compiler that we want the `sum()` method in the parent instead of the one in the child class.

The use cases of the `super` keyword

We can use the `super` keyword for three different orders in Java:

– Using the `super` keyword to access data members (fields) of the parent class

– Using the `super` keyword to access methods of parent class when child class has overridden the method

– Using the `super` keyword to call the immediate parent constructor. This is covered in the Java inheritance and constructors section.

How to use the `super` keyword in Java?

To use the `super` keyword, we attach it in front of a member name of a parent class when we want to call that member in a child class.

Example:

super.member_name;

Notes:

  • This member can be either field or a method name.
  • The `super` keyword can be called as if it’s a method like `super()`. This is mainly used in the constructor methods of a class when that class inherited from another class. In the Java inheritance constructor section, we will explain this use of `super` keyword in substantial details.

Example: Using the `super` keyword to access methods of parent class when child class has overridden the method

A method of a parent class can be overridden in a sub-class. In such case, if we create an object of the type sub-class and call the overridden method, the body of this overridden method will be executed instead of the same method in the parent class.

But what if we still want to call the same method of the parent class as well?

For example:

First classe:

public class Employee {
    public void jobDescription()  {
        System.out.println("Hi, I'm an employee :)");
    }
}

Second class:

public class Manager extends Employee{
    @Override public void jobDescription() {
        System.out.println("Hi, I'm the manager of the company.");
    }
}

The main class:

public class Simple {
    public static void main(String[] args) {
        Manager manager = new Manager();
        manager.jobDescription();
        
    }
}

Output: Hi, I'm the manager of the company.

Here we’ve overridden the `jobDescription()` method in the `Manager` class and so every time we create an object of type `Manager` and call this method, the body of the overridden method will be executed.

Now let’s say any time a call to the overridden `jobDescription()` method happened, we want the same method in the parent class to also be executed.

This is where the `super` keyword can help.

Via the `super` keyword we can call any accessible member of the immediate ancestor class that has the member.

This is how we use the keyword:

super.memberName;

Let’s refactor the overridden `jobDescription()` method of the `Manager` class to use this `super` keyword:

public class Manager extends Employee{
    @Override public void jobDescription() {
        super.jobDescription();
        System.out.println("I'm the manager of the company.");
    }
}

As you can see within the body of this overridden method, we want to access the `jobDescription()` method of the immediate ancestor that has such method. In this example the `Employee` class has this method and so the `super.jobDescription()` will invoke it.

Let’s run the example again:

public class Simple {
    public static void main(String[] args) {
        Manager manager = new Manager();
        manager.jobDescription();
    }
}

Output:

Hi, I'm an employee 🙂

I'm the manager of the company.

Example: Java super keyword and method overriding

Create a class named `Parent` with this body:

public class Parent {
    public void printName(){
        System.out.println("Parent");
    }
}

Create another class named `Middle` with this body:

public class Middle extends Parent {
    @Override
    public void printName(){
        System.out.println("Middle");
    }
}

Here, the `Middle` class derived from the `Parent` class and overrode the `printName()` of that class.

Now create the third class named `Child` with this body:

public class Child extends Middle {
    @Override
    public void printName(){
        super.printName();
        System.out.println("Child");
    }
}

This class also extended to the `Middle` class and then overrode the `printName()` class as well.

Here within the body of this method we also used the `super` keyword to call the `printName()` method of the immediate ancestor that defined this method.

Note: from the perspective of the `Child` class, the immediate class that has such method is the `Middle` class.

Now let’s create an object from the `Child` class and call the `printName()` method via this object:

public class Simple {
    public static void main(String[] args) {
        Child child = new Child();
        child.printName();
    }
}

Output:

Middle

Child

Note: the immediate ancestor that had the `printName()` method was the `Middle` class and so a call to that method inside the `Middle` class happened as well and we got the value `Middle` printed on the screen.

Leave a Reply