Skip to main content

Inheritance & Polymorphism

Side‑by‑side Java snippet · similarity & difference
🧬 Inheritance extends
// Parent class
class BankAccount {
  protected double balance;

  public void deposit(double amt) {
    balance += amt;
    System.out.println("Deposited: " + amt);
  }
}

// Child class inherits structure + behavior
class SavingsAccount extends BankAccount {
  private double interestRate;

  public void addInterest() {
    balance += balance * interestRate;
  }
}

// Usage – reuse via inheritance
SavingsAccount sa = new SavingsAccount();
sa.deposit(500);   // inherited method
sa.addInterest(); // own method
🎭 Polymorphism @Override
// Parent class defines common interface
class BankAccount {
  protected double balance;

  public void deposit(double amt) {
    balance += amt;
    System.out.println("Deposited: " + amt);
  }

  // overridden by subclasses
  public void accountType() {
    System.out.println("Generic account");
  }
}

class SavingsAccount extends BankAccount {
  @Override
  public void accountType() {
    System.out.println("Savings account");
  }
}

// Same call → different behavior
BankAccount acc = new SavingsAccount();
acc.accountType(); // "Savings account"

✅ Similarities

  • Both use the extends keyword to build a parent–child relationship.
  • Both rely on the same parent class (BankAccount) as a shared blueprint.
  • Child class (SavingsAccount) automatically inherits fields like balance and methods like deposit().
  • Both enable code reuse — common logic lives only in the superclass.
  • Both are core pillars of OOP and appear together in real class diagrams.

⚠️ Differences

  • Purpose: Inheritance = reuse structure; Polymorphism = vary behavior.
  • Mechanism: Inheritance uses extends; polymorphism uses @Override (method overriding).
  • Behavior: Inherited method deposit() runs identically in child; overridden accountType() runs differently.
  • Reference type: Polymorphism allows BankAccount acc = new SavingsAccount() — inheritance alone does not.
  • Diagram symbol: Inheritance = hollow triangle ▷; Polymorphism = often shown via override notes or dynamic dispatch.
🔑 Key insight: The two snippets share the same parent class and inheritance structure — but the right side adds overriding, turning a static “is‑a” relationship into a dynamic, interchangeable behavior. Inheritance gives you the blueprint; polymorphism lets you swap the behavior behind a common interface.

Comments

Popular posts from this blog

Utk yg mo Bantu2 Keuangan saya
..monggo ke Bank Central Asia BCA 5520166779 a.n. Andreas Tparlaungan Manurung (Indonesia)


For those who would like to help support my finances
..please feel free to send it to Bank Central Asia (BCA) account number 5520166779 under the name Andreas Tparlaungan Manurung (Indonesia)

ANDREAS TOMMY PARLAUNGAN MANURUNG SHARED POOLING ACCOUNT MY ANDROID APKs PAGE please download here! REFRESH PAGE aka CHECK LATEST UPDATE! DOWNLOAD "SHOWING" POOL OF MY ANDROID-APK(s) aka APK CONTAINING LIST OF ALL MY ANDROID-APK(s) APP CLICK HERE FOR ALWAYS BEING UPDATED FOR MY LATEST APK! CONTOH HASIL "PROGRAM" App: Prompts' Guide aka TEMPLATE-HELPERs click here to download! Youtube and Instagram EMBEDded to Blogger/Blogspot.com SOURCE CODE Click this box to download 📥 TikTok EMBEDded to Blogger/Blogspot.com SOURCE CODE Input: BrowserLINK (mandatory) Click this box to download SHORTCUT-APPs note :  "precise" click to download R8: ronin1985.blogspot.com R2M: ronin-manu.blogspot.com Helping Download(ing) OnlineVIDEO! ...

[ERROR BUG]
ChatGPT+Gemini: TikTok → Blogger Embed Converter using Cloudflare/Online Server

🔄 Refresh Page ERROR BUG: The connection is blocked because it was initiated by a public page to connect to devices or servers on your local network. Planning: Revise Program CODE Code USING Javascript/Online Server Code NOT USING Javascript Sample Working Code aka Already Repaired! Temporary Solution is by Asking AI Assistant to do REPAIR CODE of (Not yet Repaired) Current Conversion Program Code-Output TikTok Archive – Embedded Preview TikTok Embed ▶ View this video on TikTok ⚠️ DISCLAIMER: INPUT URL LIMITATION This program is currently restricted to processing Full Browser URLs only. It does not support TikTok’s mobile "short-link" format (e.g., vt.tiktok.com ). Required Action: Users must open the video in a web browser and copy the expanded URL from the address bar before pasting it into this program. URL Conversion Example ❌ UNSUPPORTED: https://vt.tiktok.com/ZSaXoFyov/ ✅ REQ...

REPOST: Studying WATER PUMP by ROMAN ENGINEERING

*^ Ini yg Asli Gan! Mekanisme pada Concrete Pump: Kok ky Sistem Mekanik Romawi ya?! Tapi malah bisa HANYA pake PER aka bukan "MATA BOR look a like" Mekanisme Drill yg Cost Pembuatan bikin REPOT aka harus Tool SUPER Khusus Dari Material Besi yg digunakan terlihat langsung secara kasat mata Jauh Lebih Banyak drpd Per Biasa seperti yg ditunjukkan pd Video Alternatif dgn Penggunaan PER Video dr Instagram: Source: YouTube Rome's drainage machines #history #romanempire #engineering