Java Super Keyword
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
| Use | Example | Purpose |
|---|---|---|
super(...) | super(holder, balance); | Run the parent's constructor |
super.method() | super.describe() | Call the parent's version of an overridden method |
super.field | super.type | Reach a parent field hidden by a child field of the same name |
All Three in One Program
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());
}
}Srihitha has Rs. 12000 in a Savings account (parent type: Account)How it works
- Line 19:
super(holder, balance)runsAccount's constructor, which sets up the inherited fields. - Line 24:
super.describe()calls the parent'sdescribe(), even though the child has overridden it. The child adds to the result instead of repeating the parent's code. - The child declares its own
typefield, which hides the parent's. Plaintypemeans the child's ("Savings");super.typereaches 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
| Rule | Detail |
|---|---|
| Calls the direct parent's constructor | Not the grandparent's |
| One per constructor | Either super(...) or this(...), not both |
| Added automatically | If you write neither, the compiler inserts super() with no arguments |
| Position | Before 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
- Easy: Write
Manager extends EmployeewhereManager's constructor callssuper(name)and adds ateamSize. - Harder: Override
toString()inManagerso it returns the parent'stoString()result plus the team size, usingsuper.toString().
Key Takeaways
Was this tutorial helpful?