<aside> 👪

L3.1 — Inheritance introduces why inheritance exists (avoiding code duplication between similar classes), the superclass/subclass relationship, the extends keyword, inheritance hierarchies, the four access modifiers (public, protected, default, private), how constructors work with super(), substitution (upcasting), and the Object class at the root of every Java class.

</aside>

📋 At a Glance


1. Why Inheritance?

The Problem: Code Duplication

Even with proper encapsulation, similar classes end up duplicating code. Consider music CDs and film DVDs — both have title, comment, gotIt, plus getters/setters and a print() method.

public class DVD {
    private String title;
    private String director;
    private String comment;
    // constructor, getters, setters, print()...
}

public class CD {
    private String title;
    private String artist;
    private String comment;
    // constructor, getters, setters, print()...
}

Duplicated code is bad because: