0/0
OOP Principles

OOP Principles Review

Consolidate the four pillars — Inheritance, Polymorphism, Abstraction, and Encapsulation. See how they depend on each other, review key patterns, and tackle cross-principle challenges that combine multiple concepts in a single hierarchy.

Explore Topics

🏗
Inheritance Recap
IS-A relationships, extends, super, constructor chaining
🔍 Principle Scanner — Spot All Four Pillars in One Hierarchy
Payment System — Click any highlighted line
1
interface Payable {
2
    boolean charge(double amount);
3
}
4
 
5
abstract class PaymentMethod implements Payable {
6
    private String holder;
7
    private double totalCharged;
8
 
9
    public PaymentMethod(String holder) {
10
        this.holder = holder;
11
    }
12
 
13
    public final boolean charge(double amt) {
14
        if (amt <= 0) return false;
15
        if (!authorize(amt)) return false;
16
        totalCharged += amt;
17
        return true;
18
    }
19
 
20
    protected abstract boolean authorize(double amt);
21
}
22
 
23
class CreditCard extends PaymentMethod {
24
    private double limit;
25
 
26
    public CreditCard(String h, double limit) {
27
        super(h);
28
        this.limit = limit;
29
    }
30
 
31
    @Override
32
    protected boolean authorize(double amt) {
33
        return amt <= limit;
34
    }
35
}
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/5
class 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

How the Four Pillars Relate
Loading diagram...
Principle Application Layers
Loading diagram...

Practice

Classify each concept or keyword under the OOP principle it primarily belongs to:
EXERCISE 1/6
extends 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/6
Abstraction 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/6
abstract 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/6
Click on the line you think contains the bug, then press Confirm.
1abstract class Vehicle {
2 public abstract String type();
3}
4class Car extends Vehicle {
5 protected String type() {
6 return "Car";
7 }
8}
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/6
Drag 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!"; }
}

References

📋

Oracle Java Tutorials — Object-Oriented Programming Concepts

docs.oracle.com

docs
📚

Effective Java, 3rd Edition — Joshua Bloch (Items 15-20)

oreilly.com

book
📚

Head First Object-Oriented Analysis and Design (O'Reilly)

oreilly.com

book
📄

Baeldung — OOP Concepts in Java

baeldung.com

article
📄

GeeksforGeeks — Four Pillars of Object-Oriented Programming

geeksforgeeks.org

article