Super Keyword in Java (original) (raw)

The super keyword in Java is used to refer to the immediate parent class object in an inheritance hierarchy. It allows a subclass to explicitly access parent class members when they are hidden or overridden. This keyword helps maintain clarity and control while working with inheritance.

Use of the super Keyword in Java

Super keywords are mainly used in the following contexts, which are listed below:

**1. Use of super with Variables

This scenario occurs when a derived class and base class have the same data members. In that case, there is a possibility of ambiguity for the JVM.

**Example: Suppose there is a child, whose name is "Max" and the child has also a parent named "Max". Normally, to refer to the parent, we would say "parent Max", this is similar to using super.maxSpeed.

Java `

// Base class vehicle class Vehicle { int maxSpeed = 120; }

// sub class Car extending vehicle class Car extends Vehicle { int maxSpeed = 180;

void display()
{
    // print maxSpeed from the vehicle class 
    // using super
    System.out.println("Maximum Speed: "
                       + super.maxSpeed);
}

}

// Driver Program class Test { public static void main(String[] args) { Car small = new Car(); small.display(); } }

`

**Explanation: In the above example, both the base class and subclass have a member **maxSpeed. We could access the maxSpeed of the base class in subclass using super keyword.

**2. Use of super with Methods

This is used when we want to call the parent class method. So, whenever a parent and child class have the same-named methods then to resolve ambiguity we use the super keyword.

**Example: It is simply just like when we want to listen to our parents' advice instead of our own decision, super.methodName() helps us follow the parents' behavior in code.

Java `

// superclass Person class Person { void message() { System.out.println("This is person class\n"); } }

// Subclass Student class Student extends Person { void message() { System.out.println("This is student class"); }

// Note that display() is
// only in Student class
void display()
{
    // will invoke or call current
    // class message() method
    message();

    // will invoke or call parent
    // class message() method
    super.message();
}

}

// Driver Program class Test { public static void main(String args[]) { Student s = new Student();

    // calling display() of Student
    s.display();
}

}

`

Output

This is student class This is person class

**Explanation: In the above example, we have seen that if we only call method **message() then, the current class message() is invoked but with the use of the super keyword, message() of the superclass could also be invoked.

**3. Use of super with Constructors

The super keyword can also be used to access the parent class constructor. One more important thing is that ‘super’ can call both parameterized as well as non-parameterized constructors depending on the situation.

**Example: Before a child born, first the parent exists. Similarly, the parent class constructor must be called before the child's constructor finishes it's work.

Java `

// superclass Person class Person { Person() { System.out.println("Person class Constructor"); } }

// subclass Student extending the Person class class Student extends Person { Student() { // invoke or call parent class constructor super();

    System.out.println("Student class Constructor");
}

}

// Driver Program class Test { public static void main(String[] args) { Student s = new Student(); } }

`

Output

Person class Constructor Student class Constructor

**Explanation: In the above example, we have called the superclass constructor using the keyword "super" via subclass constructor.

**Example: How to modify parent methods result

Java `

class ParentClass { public boolean isTrue() { return true; } }

class ChildClass extends ParentClass { public boolean isTrue() { // calls parent implementation of isTrue() boolean parentResult = super.isTrue(); // negates the parent result return !parentResult; } }

public class Main { public static void main(String[] args) { ChildClass child = new ChildClass();

    // calls child implementation
    // of isTrue()
    boolean result = child.isTrue();

    System.out.println(result);
}

}

`

**Explanation: In the above example, the child class changes the behavior of the parent class isTrue() method. It calls the parent's method using super keyword and changes the result, that's why the result is false.

Advantages of Using Java Super Keyword

The advantages of super keyword are listed below:

**Note: The super keyword is a key feature of inheritance and polymorphism in Java and it provides several benefits for developers seeking to write reusable, extensible and well-organized code.

Important Points