Back to Java
2026-02-235 min read

super (Java)

Learn super (Java) step by step with clear examples and exercises.

Why This Matters

Understanding the super keyword is essential for mastering object-oriented programming in Java. It allows you to effectively use inheritance and method overriding, two fundamental concepts in Java programming. By learning how to use super, you'll be able to write more flexible, maintainable, and efficient code.

Prerequisites

To fully grasp the super keyword, it is recommended that you have a solid understanding of the following topics:

  1. Basic Java syntax and data types
  2. Object-oriented programming concepts (classes, objects, inheritance)
  3. Method overriding in Java
  4. Understanding constructors and their usage in Java
  5. Access modifiers (public, private, protected, default)

If you're new to these topics, we recommend reviewing them before diving into the super keyword.

Core Concept

Understanding the super Keyword

The super keyword in Java serves several purposes:

  1. Invoking the superclass constructor: When a subclass is instantiated, it first calls its immediate superclass's constructor using super(). This ensures that all fields and initializations of the superclass are set up correctly before the subclass takes over.
  1. Accessing members of the superclass: You can use super to call a method or access a variable from the superclass within the subclass, even if the same method or variable is overridden in the subclass.
  1. Invoking an overridden method from the superclass: If you want to call the original implementation of an overridden method from the superclass within the subclass, use super.
  1. Accessing private members of the superclass (within the same package): While you cannot access private members of the superclass directly, you can do so using the super keyword if both the superclass and the subclass are in the same package.

Invoking Superclass Constructors

When creating a new instance of a subclass, you often need to initialize both the subclass and its superclass. To do this, you can call the superclass constructor using the super() keyword:

public class Animal {
String name;

public Animal(String name) {
this.name = name;
}
}

public class Dog extends Animal {
int age;

public Dog(String name, int age) {
super(name); // Call the Animal constructor with the given name
this.age = age;
}
}

In the example above, when a new Dog object is created, its constructor calls the Animal constructor using super(name), ensuring that both classes are properly initialized. The subclass also has an additional instance variable, age.

Accessing Superclass Members

You can access members (methods or variables) of the superclass within the subclass using the super keyword:

public class Animal {
String name;

public void makeSound() {
System.out.println("The animal makes a sound.");
}
}

public class Dog extends Animal {
public void makeSound() {
super.makeSound(); // Call the makeSound method from the Animal class
System.out.println("Woof!");
}
}

In this example, when you call dog.makeSound(), it first executes the makeSound method from the Animal class (using super.makeSound()) and then adds "Woof!" to the output.

Invoking an Overridden Method from the Superclass

If you want to call the original implementation of an overridden method from the superclass within the subclass, use super:

public class Animal {
public void eat() {
System.out.println("The animal is eating.");
}
}

public class Cat extends Animal {
@Override
public void eat() {
super.eat(); // Call the original eat method from the Animal class
System.out.println("Cats also love to eat fish.");
}
}

In this example, when you call cat.eat(), it first executes the original implementation of the eat() method from the Animal class (using super.eat()) and then adds "Cats also love to eat fish." to the output.

Worked Example

Let's create a simple inheritance hierarchy with classes Shape, Circle, and Square. The Shape class has a method called area(), which we want to override in both subclasses and calculate their respective areas.

public abstract class Shape {
public abstract double area();
}

public class Circle extends Shape {
private final double radius;

public Circle(double radius) {
this.radius = radius;
}

@Override
public double area() {
return Math.PI * Math.pow(radius, 2);
}
}

public class Square extends Shape {
private final double sideLength;

public Square(double sideLength) {
this.sideLength = sideLength;
}

@Override
public double area() {
return sideLength * sideLength;
}
}

Now, when you create instances of Circle and Square, their respective area() methods will be executed:

public static void main(String[] args) {
Shape circle = new Circle(5);
Shape square = new Square(6);

System.out.println("Circle area: " + circle.area()); // Output: Circle area: 78.53981633974483
System.out.println("Square area: " + square.area()); // Output: Square area: 36.0
}

Common Mistakes

  1. Not calling the superclass constructor: If you forget to call super() in your subclass constructor, you'll get a compile-time error.
  1. Invoking an overridden method without using super: When you override a method and want to call its original implementation from the superclass, use super. Otherwise, you'll end up calling the overridden method in your subclass repeatedly.
  1. Misusing super: Avoid using super unnecessarily or incorrectly. For example, don't use it to access instance variables directly (since they can be shadowed by variables of the same name in the subclass).
  1. Not calling the overridden method from the superclass constructor: If you override a method in the subclass and want to call it from the constructor, make sure to do so after initializing any instance variables that are used within the method.

Practice Questions

  1. Write a simple inheritance hierarchy with classes Vehicle, Car, and Truck. Override the move() method in both subclasses and calculate their respective movements (e.g., driving, hauling).
  1. A class Person has instance variables for name, age, and gender. Create a subclass Employee that extends Person. Add an instance variable salary to the Employee class and override the toString() method to display all relevant information about an employee.
  1. A class Rectangle has instance variables for width and height. Create a subclass Square that extends Rectangle. Override the area() method in the Square class to calculate its area (since a square is a special case of a rectangle with equal sides).

FAQ

  1. Can I call a constructor of another superclass using super?

No, you can't directly call a constructor of a different superclass using super. However, you can chain constructors within the same inheritance hierarchy by calling the next superclass's constructor in each step.

  1. Can I use super to access private members of the superclass?

No, you cannot access private members of the superclass using super from the subclass. Private members are only accessible within their own class.

  1. What happens if I don't call any constructor in my subclass?

If you don't explicitly call a constructor (either your own or super()) in your subclass, the default no-argument constructor will be called automatically. However, this might not initialize all members of the superclass correctly, leading to runtime errors.

  1. Can I use super to call a method from an interface?

No, you cannot use super to call a method from an interface since interfaces do not have constructors or instance variables. Instead, you'll implement the methods in your class and can call them using the regular method syntax.

super (Java) | Java | XQA Learn