- Get link
- X
- Other Apps
Inheritance vs Polymorphism
๐ Word + ๐ป Code · code panel is 4× wider than the phone screen → swipe
| ๐งฌ Inheritance explanation | ๐ญ Polymorphism example code |
|---|---|
| ✅ MAIN FOCUS: Both concepts share the same parent class structure — the difference is how the method behaves when called. | |
|
1 · Shared blueprint Inheritance lets a child class reuse the attributes and methods of a parent class. The child declares the relationship with extends and inherits balance
and deposit() automatically.
|
// Parent class – shared blueprint class BankAccount { protected double balance; public void deposit(double amt) { balance += amt; System.out.println("Deposited: " + amt); } } // Child class reuses parent structure class SavingsAccount extends BankAccount { private double interestRate; public void addInterest() { balance += balance * interestRate; } } // Usage – inherited method runs unchanged SavingsAccount sa = new SavingsAccount(); sa.deposit(500); // inherited from BankAccount sa.addInterest(); // own method |
|
2 · Same parent class Polymorphism also starts from the same parent class — it does not replace inheritance, it builds on top of it. The parent defines a common method ( accountType()) that every
subclass is expected to provide.
|
// Parent defines a common method signature class BankAccount { protected double balance; public void deposit(double amt) { balance += amt; System.out.println("Deposited: " + amt); } // overridden by each subclass public void accountType() { System.out.println("Generic account"); } } class SavingsAccount extends BankAccount { @Override public void accountType() { System.out.println("Savings account"); } } |
|
3 · Reusing structure The child object sa can call deposit() even though the method is not
written inside SavingsAccount. This is the shared reuse that
both concepts depend on.
|
// Same call → different behavior at runtime BankAccount acc = new SavingsAccount(); // Calls the overridden version inside SavingsAccount acc.accountType(); // Output: "Savings account" // Reference type = parent, object type = child // This is dynamic dispatch / runtime polymorphism BankAccount acc2 = new CurrentAccount(); acc2.accountType(); // Output: "Current account" |
|
4 · Where they differ Inheritance is about reusing the same code. Polymorphism is about replacing the code behind the same method name. In the right snippet, @Override swaps the parent behavior with the child’s version.
|
// Parent method – default behavior public void accountType() { System.out.println("Generic account"); } // Child overrides → new behavior, same signature @Override public void accountType() { System.out.println("Savings account"); } // Polymorphic list – each element responds differently List<BankAccount> accounts = new ArrayList<>(); accounts.add(new SavingsAccount()); accounts.add(new CurrentAccount()); for (BankAccount a : accounts) { a.accountType(); // different output per object } |
|
5 · Class diagram view In a class diagram, both appear on the same structure: a hollow triangle ▷ from child to parent. The difference is that polymorphism is annotated
by the {overrides} tag on the child’s method.
|
// Inheritance diagram BankAccount △ │ (hollow triangle = extends) SavingsAccount // Polymorphism on the same diagram BankAccount △ │ SavingsAccount accountType() {overrides} ← added tag // Same arrow, different annotation → relationship vs behavior // Inheritance = structure | Polymorphism = runtime behavior |
๐ Key insight: The word column explains the concept; the code column
is 400vw wide (4× the mobile screen) and scrolls horizontally. Inheritance and polymorphism
share the same parent class — inheritance reuses it, polymorphism varies it.
Comments