00:00

super Keyword in Java – Uses and Examples

Last updated: 6 min read

The super keyword lets a subclass reach the members of its parent class (its superclass). With it, a child class can call the parent's version of an overridden method, run the parent's constructor, or read a parent field hidden by one of its own. A related form, Interface.super, calls an interface's default method.

This tutorial explains each use of super with runnable examples and compares it with this.

1. Uses of super

UseSyntax
Call the parent's version of a methodsuper.methodName(args)
Call a parent constructorsuper(args); as the first statement of a constructor
Access a parent field hidden by a child fieldsuper.fieldName
Call a specific interface's default methodInterfaceName.super.methodName()

2. Calling the Parent's Method

When a subclass overrides a method, the parent's version is still available through super. This lets the child extend behaviour instead of rewriting it:

SuperMethod.java
public class SuperMethod {
    static class Notification {
        String format(String message) {
            return "[INFO] " + message;
        }
    }

    static class UrgentNotification extends Notification {
        @Override
        String format(String message) {
            // reuse the parent's version, then extend it
            return super.format(message).toUpperCase() + " !!!";
        }
    }

    public static void main(String[] args) {
        System.out.println(new Notification().format("Server restarted"));
        System.out.println(new UrgentNotification().format("Disk almost full"));
    }
}
Output
[INFO] Server restarted
[INFO] DISK ALMOST FULL !!!

Without super., the call format(message) inside the override would call itself and recurse forever.

3. Calling the Parent's Constructor: super(...)

Every constructor must start by running a parent constructor. super(...) chooses which one and passes it arguments, so the parent's fields are initialised before the child's:

SuperConstructor.java
public class SuperConstructor {
    static class Account {
        protected final String owner;
        protected double balance;

        Account(String owner, double opening) {
            this.owner = owner;
            this.balance = opening;
            System.out.println("Account(" + owner + ") created");
        }
    }

    static class SavingsAccount extends Account {
        private final double rate;

        SavingsAccount(String owner, double opening, double rate) {
            super(owner, opening);              // initialise the parent part first
            this.rate = rate;
            System.out.println("SavingsAccount with " + rate + "% interest");
        }

        double yearlyInterest() { return balance * rate / 100; }
    }

    public static void main(String[] args) {
        SavingsAccount s = new SavingsAccount("Ravi", 10_000, 4.5);
        System.out.println(s.owner + " earns " + s.yearlyInterest() + " per year");
    }
}
Output
Account(Ravi) created
SavingsAccount with 4.5% interest
Ravi earns 450.0 per year
  • super(...) must be the first statement in the constructor (Java 25 allows some simple statements before it that do not use this).
  • If you do not write it, the compiler inserts super();, which fails if the parent has no no-argument constructor.
  • A constructor can call either super(...) or this(...), not both.

See Constructor Chaining for details.

4. Accessing a Hidden Parent Field

If a subclass declares a field with the same name as a parent field, the child's field hides the parent's. super.field still reaches the parent's one:

SuperField.java
public class SuperField {
    static class Vehicle {
        String type = "vehicle";
        int wheels = 4;
    }

    static class Bike extends Vehicle {
        String type = "bike";                    // hides the parent's field (avoid in real code)
        int wheels = 2;

        void show() {
            System.out.println("type:  " + type + " / super.type:  " + super.type);
            System.out.println("wheels: " + wheels + " / super.wheels: " + super.wheels);
        }
    }

    public static void main(String[] args) {
        new Bike().show();
    }
}
Output
type:  bike / super.type:  vehicle
wheels: 2 / super.wheels: 4
Note: field hiding is legal but confusing. Fields are not polymorphic like methods, so which one you get depends on the reference type. Avoid reusing parent field names in subclasses.

5. Interface.super for Default Methods

When a class implements two interfaces that provide the same default method, Java forces the class to override it. Inside the override, InterfaceName.super.method() calls a specific interface's version:

SuperInterfaceDefault.java
public class SuperInterfaceDefault {
    interface Walker  { default String move() { return "walking"; } }
    interface Swimmer { default String move() { return "swimming"; } }

    // Two interfaces with the same default method: the class must choose
    static class Duck implements Walker, Swimmer {
        @Override
        public String move() {
            return Walker.super.move() + " and " + Swimmer.super.move();   // Interface.super.method()
        }
    }

    public static void main(String[] args) {
        System.out.println("Duck is " + new Duck().move());
    }
}
Output
Duck is walking and swimming

6. super vs this

Aspectthissuper
Refers toThe current object (its own class)The current object, viewed as its parent class
Method callthis.m() uses dynamic dispatch (may call a subclass override)super.m() always calls the parent's version
Constructor callthis(...): same classsuper(...): parent class
Can be passed or returnedYes (return this;)No; super is not a standalone value
Static contextNot allowedNot allowed

7. Rules

  • super can only be used in instance methods, constructors and instance initialisers.
  • You cannot chain it to skip a level: super.super.method() is not allowed.
  • super.method() cannot call a private parent method, because private methods are not inherited.
  • super cannot be assigned, returned or printed on its own.

8. Summary

  • super.method() calls the parent's version of an overridden method.
  • super(...) calls a parent constructor and runs before the rest of the child constructor.
  • super.field accesses a parent field hidden by a child field.
  • Interface.super.method() picks a specific interface's default method.
  • super works only in instance contexts and cannot skip levels.

Related Tutorials