Sub topic 6
Extending Behavior with super
When you override a method, the new version replaces the old one. Sometimes you don't want to throw the old version away. You want to keep what it does and add a little more. The super keyword lets you do that.
What super Means
super is a Java keyword that refers to the parent class of the current object. When you write super.describe(), Java runs the parent's version of describe(). It skips the override in the current class.
This matters because a plain call to describe() inside the subclass would run the subclass's own version. If that call sits inside the override itself, the method calls itself forever. super.describe() avoids that and reaches the version one level up.
A Worked Example
A bank has a general Account class. A SavingsAccount is a special kind of account. Its description should say everything a normal account says, plus a note that it is a savings account.
class Account {
String describe() {
return "Account";
}
}
class SavingsAccount extends Account {
@Override
String describe() {
return super.describe() + " (savings)";
}
}
public class Main {
public static void main(String[] args) {
Account plain = new Account();
Account savings = new SavingsAccount();
System.out.println(plain.describe());
System.out.println(savings.describe());
}
}Output:
Account
Account (savings)Here is what happens when you call savings.describe():
- The object is a
SavingsAccount, so Java runsSavingsAccount.describe(). - That method calls
super.describe(), which runsAccount.describe(). Account.describe()returns"Account".- The override adds
" (savings)"and returns the result.
Why This Is Useful
Reusing the parent's code with super has two benefits:
- No copying. You don't paste the parent's logic into the child. Copied code goes stale when someone edits only one copy.
- Changes flow down. If the bank later changes
Account.describe()to return"Bank Account", the savings version becomes"Bank Account (savings)"without any edit.
Where You Can Put the super Call
The super.method() call can go anywhere inside the method body. You can call it first, last, or in the middle, depending on what you need.
| Placement | Typical use |
|---|---|
| At the start | Do the parent's work, then add more |
| At the end | Prepare something, then hand off to the parent |
| Inside an `if` | Use the parent's behavior only in some cases |
Exercise
Write a class PremiumSavingsAccount that extends SavingsAccount and overrides describe() so that new PremiumSavingsAccount().describe() returns "Account (savings) [premium]". Use super instead of typing the full text.
Show answer
class PremiumSavingsAccount extends SavingsAccount { @Override String describe() { return super.describe() + " [premium]"; } } The super call runs SavingsAccount.describe(), which itself calls Account.describe(), giving "Account (savings)" before " [premium]" is added.
Exercise
Inside SavingsAccount, what goes wrong if you write return describe() + " (savings)"; instead of using super.describe()?
Show answer
The call describe() runs SavingsAccount's own describe() again, so the method keeps calling itself. The program crashes with a StackOverflowError. super.describe() is needed to reach Account's version.