super Keyword in Java – Uses and Examples
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
| Use | Syntax |
|---|---|
| Call the parent's version of a method | super.methodName(args) |
| Call a parent constructor | super(args); as the first statement of a constructor |
| Access a parent field hidden by a child field | super.fieldName |
| Call a specific interface's default method | InterfaceName.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:
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"));
}
}
[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:
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");
}
}
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 usethis).- 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(...)orthis(...), 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:
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();
}
}
type: bike / super.type: vehicle wheels: 2 / super.wheels: 4
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:
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());
}
}
Duck is walking and swimming
6. super vs this
| Aspect | this | super |
|---|---|---|
| Refers to | The current object (its own class) | The current object, viewed as its parent class |
| Method call | this.m() uses dynamic dispatch (may call a subclass override) | super.m() always calls the parent's version |
| Constructor call | this(...): same class | super(...): parent class |
| Can be passed or returned | Yes (return this;) | No; super is not a standalone value |
| Static context | Not allowed | Not allowed |
7. Rules
supercan 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 aprivateparent method, because private methods are not inherited.supercannot 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.fieldaccesses a parent field hidden by a child field.Interface.super.method()picks a specific interface's default method.superworks only in instance contexts and cannot skip levels.
Related Tutorials
Frequently Asked Questions (FAQ)
Q1. What is the super keyword in Java?
A keyword that refers to the parent class's members from a subclass: its methods, constructors and fields.
Q2. What is super() used for?
To call a constructor of the parent class from a subclass constructor, so the inherited part of the object is initialised first.
Q3. Is super() called automatically?
Yes. If a constructor does not start with this(...) or super(...), the compiler inserts super();.
Q4. Why must super() be the first statement?
So the parent part of the object is initialised before the child code uses it. Java 25 relaxes this slightly, allowing statements before super(...) that do not use the object.
Q5. Can I call super.super.method()?
No. You can only reach the direct parent. If you need the grandparent's behaviour, the parent class must expose it.
Q6. Can super be used in a static method?
No. Like this, super requires an object, and static methods have none.
Q7. What is the difference between super and this?
this refers to the current object and uses normal dynamic dispatch. super accesses the parent class's implementation of methods and constructors.
Q8. When should I call super.method() in an override?
When the child should add to the parent's behaviour rather than replace it, for example in lifecycle methods, toString() or equals() implementations of subclasses.
Q9. Can I use super and this together in a constructor?
You cannot call both super(...) and this(...), but you can use super.method() and this.field in the same constructor body.
Q10. Can super access private members of the parent?
No. Private members are not visible to subclasses, so super cannot reach them. Use protected or public methods instead.
Q11. What is Interface.super?
Syntax for calling a specific interface's default method from a class that implements it, needed when two interfaces provide conflicting defaults.
Q12. Does every class have a super class?
Every class except Object. Classes without an extends clause implicitly extend Object, so super() calls Object().
Q13. What happens if the parent has no default constructor?
Every subclass constructor must explicitly call one of the parent's existing constructors with super(...), or compilation fails.
Q14. Is super a reference variable?
Not exactly. It is a keyword used to access parent members. Unlike this, it cannot be used as a standalone value, passed or returned.
Q15. Can I use super in an interface?
Inside an interface's default method you can use OtherInterface.super.method() to call a super-interface's default method.
Q16. What is field hiding?
When a subclass declares a field with the same name as a parent field. Both exist; super.name reaches the parent's. It is legal but confusing and best avoided.
Q17. Does super.method() use dynamic dispatch?
No. It always calls the parent class's implementation, even if the object's actual class overrides the method further down.
Q18. Can super be used inside an inner class?
Plain super refers to the inner class's own parent. To call the enclosing class's parent method, use Outer.super.method().
Q19. Do records support super?
Records cannot extend classes (they implicitly extend java.lang.Record), so super(...) calls are not used in them. Interface.super still works for default methods.
Q20. Is calling super.toString() useful?
Yes, when a subclass wants to include the parent's description in its own toString(), such as "Savings[" + super.toString() + ", rate=" + rate + "]".