The super keyword is used in different situations. Before getting started with this post we encourage you to learn about inheritance in Java for a better understanding.

What is the super keyword in Java?

Super is a keyword that can be used to invoke overridden methods of the superclass, as well as to refer to hidden fields of the superclass.

Why and when to use the super keyword?

The Java super keyword has three explicit uses.
  1. Access the data members of the parent class when the child class also has data members with the same name.
  2. Call the default or parameterized constructors of the parent class in the child class.
  3. Call the parent class methods in the child class if the child has overridden methods.
Let us understand all of the above three cases using the help of examples.

Example 1 - Access parent’s data members

Example 1 illustrates how the attributes or data members of the Vehicle class can be accessed in the Car-type child class. Make sure you run the snippet below to understand well.

class Vehicle {

	String name = "vehicle";
}

class Car extends Vehicle {

	String name = "car";

	public void printMyName() {

		System.out.println(name);
	}

	public void printParentName() {

		// use super keyword to access 
		// parent's data member / attribute
		System.out.println(super.name);
	}

	public static void main(String[] args) {

		Car myCar = new Car();
		System.out.print("My Car's Name: "); 
		myCar.printMyName();
		
		// printing the parent's name 
		// using the super keyword 
		System.out.print("My Parent Vehicle's Name: "); 
		myCar.printParentName();
	}
}
Output
My Car's Name: car My Parent Vehicle's Name: vehicle

Example 2 - Access the parent’s constructors in child class

Explicitly calling super() allows you to access both the default or parameterized constructor of the parent class in the child class. Here’s an example of the parameterized constructor. The parent i-e Shape class’ constructor is called (using super()) in the child i-e Triangle class to set the attributes. Run the program below to test the output for yourself.

public class Shape {

	String name;

	public Shape(String n) {

		System.out.println("Shape() parameterized constructor called!");
		name = n;
	}
}

class Triangle extends Shape {
	
	int sides = 3;
	String color;

	public Triangle(String n, String c) {
		
		// The super keyword calls the parameterized 
		// constructor of the parent (Shape) with 
		// 'n' as a parameter 
		super(n);
		
		System.out.println("Triangle() parameterized constructor called!");
		this.color = c;
	}

	public static void main(String[] args) {

		Triangle myTriangle = new Triangle("Triangle Alpha", "Yellow");
		
		System.out.println(myTriangle.name);
		System.out.println(myTriangle.color);
	}
}
Output
Shape() parameterized constructor called! Triangle() parameterized constructor called! Triangle Alpha Yellow
Quick Challenge: Redesign the above program by using the default constructor to test your learning. Also, see how super() is different from super(arg).

Example 3 - Access the parent’s overridden method in child class

Example 3 shows how you can access the methods of parent’s class that the child class also defines. The parent class Sound in the program below defines a method voice(). The child class Drum also has a method with the same name i-e voice(). It means the method voice is overridden by the subclass. Run the program below to learn how the super keyword is necessary to use the parent class’ methods in the child class.

public class Sound {

	public void voice() {
		System.out.println("Play sound!");
	}
}

class Drum extends Sound {

	public void voice() {
		System.out.println("Play drums!");
	}

	public void play() {

		// The super keyword calls the 
		// voice() method of the parent 
		super.voice();
		voice();
	}
	
	public static void main(String[] args) {

		Drum myDrum = new Drum();
		myDrum.play();
	}
}
Output
Shape() parameterized constructor called! Triangle() parameterized constructor called! Triangle Alpha Yellow

General Characteristics of super in Java

You’ve already learned how the super keyword helps us call superclass methods and constructors. But let’s dig a little deeper! Did you know that super is a reserved keyword in Java, foundational to how inheritance and polymorphism work? Let’s unpack that, step by step, and then see how super can help us access superclass instance variables. Ready? Let’s do it!

Super as a Reserved Keyword and Its Role in Inheritance and Polymorphism

The super keyword is quite literally “off-limits” for use as a variable or method name because it’s a reserved keyword in Java. At its core, super helps us navigate the inheritance chain and access the direct superclass of the current class. In other words, if your class MySubclass extends MySuperclass, then super points to the MySuperclass part of any instance of MySubclass.

But how does this tie into inheritance and polymorphism? Inheritance allows you to create child classes that extend or modify the behavior of a parent class. Polymorphism then lets those child classes be treated as instances of the parent type, but with overridden methods that reflect their specialized behavior. super is key to bridging these concepts, making it easy to call the parent’s version of a method or constructor whenever you need it. Think of it as your “backstage pass” to the superclass code.Using super with Instance Variables

Sure, calling superclass methods is handy. But what if your subclass has an instance variable with the same name as one in the superclass? That’s where super swoops in to save the day—allowing you to specify exactly which variable you want.


class Parent {
    String name = "ParentName";
}

class Child extends Parent {
    String name = "ChildName";

    void printNames() {
        System.out.println("Child's name: " + name);
        System.out.println("Parent's name: " + super.name);
    }
}

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

In this snippet, super.name refers to the Parent class’s name, while just name (without super) refers to the Child class’s name. Handy, right?

Limitations of Using super in Static Contexts

One important caveat: you can’t use super in static methods or reference it from static variables. Remember, super depends on an actual object instance to refer to the superclass portion of it. Static methods and variables belong to the class itself, not a specific instance. That means no super in static land!

Conclusion

By the end of this post, we hope you will be able to understand the working of the super keyword in Java. We encourage you to learn coding by practice. As practice is the ultimate key to learning logic building. This post will welcome you anyway whenever you get stuck. Till then, happy learning!