Lesson Completion
Back to course

The `super` Keyword: Calling Your Parents

Intermediate
18 minutes4.8Java

1. The Hook (The "Byte-Sized" Intro)

In a Nutshell: The super keyword is a reference to the parent class. It allows a child class to access parent class members (fields, methods, constructors) that might be hidden or overridden. Think of it as calling your parents when you need their help or expertise.

Think of iPhone Software Updates. The new iOS version extends the previous one, but sometimes needs to call super.oldFeature() to maintain backward compatibility while adding new features on top.


2. Conceptual Clarity (The "Simple" Tier)

💡 The Analogy: The Family Helpline

Imagine you're learning to cook:

  • You (Child): Want to make pizza but don't know the dough recipe.
  • Call super.makeDough(): You call your parent's method to use their perfected dough recipe.
  • Add Your Twist: Then you add your own special toppings.

super is the direct phone line to your parents' knowledge!

Hand-Drawn Logic Map

graph TD Child[Child Class Method] --> Decision{Need parent help?} Decision -- Yes --> Super["super.parentMethod()"] Decision -- No --> Own[Use own implementation] Super --> Parent[Parent Class Method] Parent --> Return[Returns result to Child] style Super fill:#F57C00 style Parent fill:#2E7D32

3. Technical Mastery (The "Deep Dive")

Formal Definition

The super keyword has three primary uses:

  1. Access parent fields: super.variableName
  2. Call parent methods: super.methodName()
  3. Call parent constructor: super(arguments) (must be first line in child constructor)

The "Why" Paragraph

Why need super? When a child class overrides a parent method, the original parent method becomes "hidden." But sometimes you want to extend (not replace) the parent's behavior—like adding logging before calling the original logic. super lets you "reach up" to the parent class and invoke the hidden version.

Visual Architecture

sequenceDiagram participant Child participant Parent Child->>Child: Calls own method Child->>Parent: super.parentMethod() Parent->>Parent: Executes parent logic Parent-->>Child: Returns result Child->>Child: Continues with child logic

4. Interactive & Applied Code

The "Perfect" Code Block

java
class BankAccount { String accountNumber; double balance; BankAccount(String accNum, double bal) { this.accountNumber = accNum; this.balance = bal; } void deposit(double amount) { balance += amount; System.out.println("Deposited: $" + amount); } void displayInfo() { System.out.println("Account: " + accountNumber + ", Balance: $" + balance); } } class SavingsAccount extends BankAccount { double interestRate; // 1. Calling Parent Constructor SavingsAccount(String accNum, double bal, double rate) { super(accNum, bal); // MUST be first line! this.interestRate = rate; } // 2. Overriding but EXTENDING parent behavior @Override void deposit(double amount) { super.deposit(amount); // Call parent's deposit logic first System.out.println("Interest rate: " + interestRate + "%"); } // 3. Overriding and calling parent method @Override void displayInfo() { super.displayInfo(); // Show basic info from parent System.out.println("Interest Rate: " + interestRate + "%"); } } public class Main { public static void main(String[] args) { SavingsAccount acc = new SavingsAccount("SA123", 1000, 3.5); acc.deposit(500); acc.displayInfo(); } }

The "Anti-Pattern" Example

❌ Forgetting super() in Constructor

java
class Child extends Parent { Child() { // ❌ If Parent has NO default constructor, this FAILS! // Compiler tries to insert super() automatically } }

Fix: Explicitly call super(args) if parent has only parameterized constructors.


5. The Comparison & Decision Layer

Versus Table: this vs. super

Featurethissuper
Refers ToCurrent classParent class
AccessOwn membersParent members
Constructor Callthis(args) (same class)super(args) (parent class)

6. The "Interview Corner" (The Edge)

The "Killer" Interview Question: "What happens if you call both this() and super() in a constructor?" Answer: You can't! Both must be the first line of the constructor, making it impossible to call both. You can call this() to chain to another constructor in the same class, which can then call super().

JVM Note

Constructor Chaining: Even if you don't write super(), Java automatically inserts it. The parent constructor ALWAYS runs before the child constructor (bottom-up initialization).

Pro-Tip: Use super to extend parent behavior, not replace it entirely:

java
@Override void save() { super.save(); // Keep parent's save logic logToAuditSystem(); // Add your own enhancement }

Topics Covered

Object-Oriented ProgrammingInheritance

Tags

#java#inheritance#polymorphism#super-class#sub-class#interview-prep

Last Updated

2025-02-01