Call the parent's version with super.method()
30 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)A child class changes a method, but the parent's version already does most of the job. Copying the parent's code would mean two places to fix later.
Replace or extend?
A plain override replaces the parent version: only the child's body runs. A UPI receipt should print the normal receipt plus the UPI id, without copying the parent's lines.
Example 1 · Extend a receipt instead of replacing it
class Payment { void receipt(int amount) { System.out.println("--- Receipt ---"); System.out.println("Amount: Rs. " + amount); }}
class UpiPayment extends Payment { String upiId;
UpiPayment(String upiId) { this.upiId = upiId; }
@Override void receipt(int amount) { super.receipt(amount); System.out.println("Paid by UPI: " + upiId); }}
public class ReceiptDemo { public static void main(String[] args) { UpiPayment p = new UpiPayment("aarav@okbank"); p.receipt(450); }}--- Receipt --- Amount: Rs. 450 Paid by UPI: aarav@okbank
What this code does
super.receipt(amount)runs the two parent lines first. Then the child adds its own line.- If the parent receipt changes later, the UPI receipt gets the change automatically: there is no copied code.
Copied lines go stale: when the parent's receipt changes, every copy must be found and fixed. With super.receipt(amount) the child reuses the parent's code and adds only what is new.
How super.method() works
super.method() calls the parent class's version of a method on the same object. It is used inside an override to reuse the parent's work and add to it, instead of copying the parent's code.
super. tells Java to start looking for the method in the parent class, not in the child. You can pass arguments, use the return value and call it on any line.
Example 2 · Build on the parent's return value
class Employee { int bonus(int salary) { return salary * 10 / 100; }}
class Manager extends Employee { @Override int bonus(int salary) { return super.bonus(salary) + 5000; }}
public class BonusDemo { public static void main(String[] args) { System.out.println("Employee: Rs. " + new Employee().bonus(40000)); System.out.println("Manager: Rs. " + new Manager().bonus(40000)); }}Employee: Rs. 4000 Manager: Rs. 9000
What this code does
super.bonus(salary)returns the normal 10% (4000), and the manager adds a fixed Rs. 5000.- If the company changes the normal rule in
Employee, the manager's bonus follows it.
Example 3 · The child can work before and after
class Notification { void send(String msg) { System.out.println("Message: " + msg); }}
class SmsNotification extends Notification { @Override void send(String msg) { System.out.println("Connecting to SMS gateway"); super.send(msg); System.out.println("Delivered"); }}
public class OrderDemo { public static void main(String[] args) { new SmsNotification().send("Your OTP is 4821"); }}Connecting to SMS gateway Message: Your OTP is 4821 Delivered
What this code does
- Here
super.send(msg)sits in the middle: the child works before and after it. - The order of the lines in the child decides the order of the output.
This differs from super(...) (opens in a new tab) in a constructor, which must be the first line. super. can also reach any other inherited method, such as super.log(), and no second object is created: it is the same object, running the parent's code.
The trap: forgetting super
Inside the child's pay, a plain pay(amount) means this object's pay: the child method itself. So it calls itself again and again until the call stack is full.
Example 4 · Mistake: forgetting super. causes a StackOverflowError
class Payment { void pay(int amount) { System.out.println("Paying Rs. " + amount); }}
class UpiPayment extends Payment { @Override void pay(int amount) { pay(amount); System.out.println("Sent by UPI"); }}
public class StackDemo { public static void main(String[] args) { new UpiPayment().pay(500); }}Exception in thread "main" java.lang.StackOverflowError at UpiPayment.pay(StackDemo.java:10) at UpiPayment.pay(StackDemo.java:10) at UpiPayment.pay(StackDemo.java:10) ...
What this code does
- Line 10 calls
pay(amount)withoutsuper., soUpiPayment.paycalls itself forever. The real output repeats the same line about 1,000 times. - Nothing is printed before the crash, because the first
printlnis never reached. Writesuper.pay(amount);to fix it.
This is endless recursion, as in Recursion basics (opens in a new tab): the program crashes with a StackOverflowError. The fix is one word: super.pay(amount).
super in a multilevel chain
In Account → SavingsAccount → StudentSavings, super.open() in StudentSavings calls the nearest version above it. If that version also calls super.open(), the call travels on up to Account.
Example 5 · A middle class that does not override
class Phone { void boot() { System.out.println("Power on"); }}
class SmartPhone extends Phone {}
class GamingPhone extends SmartPhone { @Override void boot() { super.boot(); System.out.println("Cooling fan on"); }}
public class SkipDemo { public static void main(String[] args) { new GamingPhone().boot(); }}Power on Cooling fan on
What this code does
SmartPhonehas noboot()of its own; it inheritsPhone's.- So
super.boot()inGamingPhonereaches the nearest version above it, which isPhone's.
You cannot skip a level: super.super.open() is not valid Java. Each class knows only its direct parent, so to reuse the grandparent's work the middle class must call super itself.
Trace the code
Follow super.open() up the chain
Variables
- this
- StudentSavings
Output
(nothing printed yet)Line 22main makes one StudentSavings object and calls open(). Its own class has open(), so that version runs.
Next step
Check your understanding
11 quick questions