Explore Topics
🏗
Inheritance Recap
IS-A relationships, extends, super, constructor chaining
🔍 Principle Scanner — Spot All Four Pillars in One Hierarchy
5
Inheritance
6
Polymorphism
5
Abstraction
8
Encapsulation
🏗
Inheritance Recap
IS-A relationships, extends, super, constructor chaining
Inheritance lets a child class reuse and extend a parent class. The child IS-A parent — a Manager IS-A Employee. Constructors chain from child to parent via super(), executing top-down. Inheritance is not just about code reuse — its primary role is to establish a type hierarchy that enables polymorphism. Without inheritance (or interface implementation), there is no polymorphic dispatch.
Key Ideas
- extends creates an IS-A relationship between child and parent
- Constructors chain: child calls super() → parent constructor runs first
- Child inherits all non-private members from parent
- Primary purpose: establish type hierarchy, not just code reuse
- Single class inheritance in Java (one extends only)
- instanceof checks the entire IS-A chain
- Inheritance ENABLES polymorphism — without it, no dynamic dispatch
Strengths
- ✦Models real-world IS-A relationships naturally
- ✦Enables polymorphism through shared type hierarchy
- ✦Code reuse — shared behavior defined once in parent
- ✦Constructor chaining ensures consistent initialization
Weaknesses
- △Tight coupling — child is bound to parent implementation
- △Fragile base class problem — parent changes can break children
- △Single inheritance limits — can only extend one class
- △Overuse leads to deep hierarchies that are hard to maintain
Used in:The Java Collections Framework: ArrayList extends AbstractList extends AbstractCollection. Each level adds behavior while inheriting from the level above.
Code Snippets
Quiz
Inheritance Recap Quiz
QUIZ 1/5class Employee {
Employee(String n, double s) {
System.out.println("Employee");
}
}
class Manager extends Employee {
Manager(String n, double s, int t) {
super(n, s);
System.out.println("Manager");
}
}
class Director extends Manager {
Director(String n, double s, int t, String d) {
super(n, s, t);
System.out.println("Director");
}
}What is the output when creating `new Director("X", 100, 5, "IT")`?
Diagrams
Practice
Classify each concept or keyword under the OOP principle it primarily belongs to:
EXERCISE 1/6extends keyword
@Override annotation
private access modifier
interface keyword
super() call
Dynamic dispatch
Defensive copying
abstract keyword
IS-A relationship
Method overloading
Immutable classes
Template Method pattern
Inheritance
Polymorphism
Abstraction
Encapsulation
Cross-Principle Myths
EXERCISE 2/6Abstraction and encapsulation are the same thing.
You can have polymorphism without inheritance in Java.
Static methods can be overridden polymorphically.
Making all fields private automatically makes a class fully encapsulated.
Inheritance is primarily for code reuse.
Constructor chaining ensures parent state is initialized before child state.
An abstract class must contain at least one abstract method.
The Liskov Substitution Principle says a subclass should be substitutable for its parent without breaking behavior.
Full Hierarchy Trace
EXERCISE 3/6abstract class Shape {
Shape() { describe(); }
abstract void describe();
}
class Circle extends Shape {
private String name = "Circle";
void describe() {
System.out.println("I am " + name);
}
}
class Main {
public static void main(String[] args) {
Shape s = new Circle();
s.describe();
}
}
Line: new Circle() → constructor chain
Line: s.describe()
Multi-Principle Bug
EXERCISE 4/6Click on the line you think contains the bug, then press Confirm.
Build a Complete Hierarchy
EXERCISE 5/6 Drawable {
void draw();
}
class Shape implements Drawable {
String color;
public Shape(String color) { this.color = color; }
protected String getColor() { return color; }
}
class Circle extends Shape {
private double radius;
public Circle(String color, double r) {
super(color);
this.radius = r;
}
public void draw() {
System.out.println("Drawing " + getColor() + " circle");
}
}
Hierarchy Assembly
EXERCISE 6/6Drag the lines into the correct order.
interface Speakable {
String speak();
}
abstract class Animal implements Speakable {
private String name;
public Animal(String name) { this.name = name; }
public String getName() { return name; }
}
class Dog extends Animal {
public Dog(String name) { super(name); }
@Override
public String speak() { return "Woof!"; }
}