<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>
extends keyword to create a subclassprotected includes subclasses even across packages; default does notsuper() to call the superclass constructor for proper encapsulationsuper() must be the first statement in the subclass constructorEven 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: