Java Super Keyword

Naveen AnamaneniUpdated · 4 min read

this means "the current object". super means "the parent's part of the current object". A subclass uses it to build its parent part, to reuse the parent's version of a method it has overridden, or to reach a parent field it has hidden with one of the same name. Three uses, one idea: go up one level.

The Three Uses

super reaches the parent's part of the same object
UseExamplePurpose
super(...)super(holder, balance);Run the parent's constructor
super.method()super.describe()Call the parent's version of an overridden method
super.fieldsuper.typeReach a parent field hidden by a child field of the same name

All Three in One Program

SuperDemo.java▶ Try it
public class SuperDemo {
    static class Account {
        String type = "Account";
        String holder;
        long balance;
 
        Account(String holder, long balance) {
            this.holder = holder;
            this.balance = balance;
        }
 
        String describe() { return holder + " has Rs. " + balance; }
    }
 
    static class SavingsAccount extends Account {
        String type = "Savings";
 
        SavingsAccount(String holder, long balance) {
            super(holder, balance);
        }
 
        @Override
        String describe() {
            return super.describe() + " in a " + type
                    + " account (parent type: " + super.type + ")";
        }
    }
 
    public static void main(String[] args) {
        System.out.println(new SavingsAccount("Srihitha", 12000).describe());
    }
}
Output
Srihitha has Rs. 12000 in a Savings account (parent type: Account)

How it works

  1. Line 19: super(holder, balance) runs Account's constructor, which sets up the inherited fields.
  2. Line 24: super.describe() calls the parent's describe(), even though the child has overridden it. The child adds to the result instead of repeating the parent's code.
  3. The child declares its own type field, which hides the parent's. Plain type means the child's ("Savings"); super.type reaches the parent's ("Account").

Try it yourself: change super.describe() to describe() in line 24. What happens when you run it, and why?

super(...) Rules

RuleDetail
Calls the direct parent's constructorNot the grandparent's
One per constructorEither super(...) or this(...), not both
Added automaticallyIf you write neither, the compiler inserts super() with no arguments
PositionBefore Java 25, the first statement; from Java 25, simple statements may come before it (see "Flexible Constructor Bodies")

The automatic super() causes a common error when the parent has no no-arg constructor (see the next lesson).

Common Mistakes

Calling describe() instead of super.describe() inside the override. The method calls itself forever and ends in StackOverflowError.

Expecting super.super.method(). There's no such thing; super reaches only the direct parent.

Using super in a static method. Like this, it needs an object.

Interview Corner

What is the super keyword used for?

super refers to the parent-class part of the current object. super(...) calls a parent constructor from a child constructor; super.method() calls the parent's version of a method the child has overridden; super.field accesses a parent field hidden by a child field with the same name.

What is the difference between this and super?

this refers to the current object as a whole, including the child's own members; this(...) calls another constructor of the same class. super refers to the inherited parent part; super(...) calls the parent's constructor and super.method() the parent's version of a method.

Practice

  1. Easy: Write Manager extends Employee where Manager's constructor calls super(name) and adds a teamSize.
  2. Harder: Override toString() in Manager so it returns the parent's toString() result plus the team size, using super.toString().

Quick check: super

4 questions
1What does super(holder, balance) do?
2Inside an overriding describe(), how do you call the parent's version?
3What does the compiler insert if a constructor calls neither this(...) nor super(...)?
4Can you write super.super.describe()?

Key Takeaways

Was this tutorial helpful?

Lesson 115 of 482