0/0
OOP Principles

Principles of OOP: Inheritance

Master the mechanism that lets classes share state and behavior — from the extends keyword and super calls to the diamond problem and constructor chaining rules that every Java developer must know.

Explore Topics
1
Sharing state and behavior through class hierarchies
Inheritance Basics
🧬 Inheritance Beam — Member Flow Visualizer
Employeesuperclass
privatefieldid
privatefieldfirstName
privatefieldlastName
publicmethodgetId()
publicmethodsetId()
publicmethodgetFirstName()
publicmethodsetFirstName()
publicmethoddescribe()
InheritedOverriddenPrivate (locked)Own member
🧬

Inheritance Basics

Sharing state and behavior through class hierarchies

Inheritance is one of the four pillars of OOP. It allows a subclass to inherit all non-private fields and methods from a superclass, promoting code reuse and reducing redundancy. The extends keyword establishes an IS-A relationship: a Manager IS-A Employee, meaning it can do everything an Employee can — and more. The subclass can add new members and override inherited behavior, but it can never remove what the parent provides.

Key Ideas
1extends keyword creates a parent-child (superclass-subclass) relationship
2Subclass inherits all non-private members (fields and methods)
3Subclass can add new members but cannot remove parent members
4IS-A relationship: Manager IS-A Employee
5Promotes code reuse and reduces redundancy
6Java supports single class inheritance only (one direct parent)
Strengths
  • ✦Eliminates duplicate code across related classes
  • ✦Establishes clear IS-A hierarchies that model real-world relationships
  • ✦Inherited methods work transparently — callers don't need to know which class defined them
  • ✦Subclasses can specialize behavior while keeping the shared foundation
Weaknesses
  • △Tight coupling between parent and child — changes to parent ripple down
  • △Deep hierarchies become hard to understand and maintain
  • △"Fragile base class" problem: parent changes can break children
  • △Inheritance is static — you can't change the parent at runtime
Used in:GUI frameworks (JPanel extends JComponent extends Container), collection hierarchies (ArrayList extends AbstractList extends AbstractCollection)

Inheritance Basics Quiz

Inheritance Basics Quiz
QUIZ 1/6
What does the "extends" keyword establish between two classes?

Practice Exercises

Classify each type of inheritance — drag each description to the correct category:
EXERCISE 1/2
Dog extends Animal
Dog and Cat both extend Animal
Puppy extends Dog extends Animal
GrandChild extends Child1, Child2
Cat extends Animal, Cat implements Comparable
Single
Hierarchical
Multi-level
Multiple (Not allowed via classes)
Hybrid
Order these events that happen when you call new ChildClass():
EXERCISE 2/2
ChildClass constructor body runs
Object() constructor runs
ParentClass constructor body runs
Memory allocated on the heap
super() call in ChildClass chains to ParentClass
Step 1
Step 2
Step 3
Step 4
Step 5