Computer Science · College · BCA, BTech & MCA
The 4 Pillars of OOP: Encapsulation, Abstraction, Inheritance & Polymorphism
Master Object-Oriented Programming principles with Java and Python: Encapsulation, Abstraction, Inheritance (and Diamond Problem), and Polymorphism (Overloading vs Dynamic Overriding).
1Pillar 1 & 2: Encapsulation and Data Hiding vs Abstraction
The 4 Pillars of OOP: Encapsulation, Abstraction, Inheritance & Polymorphism
Master Object-Oriented Programming principles with Java and Python: Encapsulation, Abstraction, Inheritance (and Diamond Problem), and Polymorphism (Overloading vs Dynamic Overriding).
- 1Subject — Computer Science
- 2Target Audience — BCA, BTech & MCA Students
- 3Topic Core — Encapsulation, Abstraction, Inheritance & Polymorphism
Encapsulationis the bundling of data attributes and the methods that operate on them into a single unit (a class), restricting direct unauthorized access to internal state viaaccess modifiers(, ).
Abstractionhides internal implementation complexity and exposes only the essential interface to the outside world, realized throughAbstract ClassesandInterfaces.
Core Distinction: Encapsulation vs Abstraction
| Dimension | Encapsulation (Data Hiding) | Abstraction (Complexity Hiding) |
|---|---|---|
| Focus | HOW data is bundled and protected from corruption | WHAT an entity does rather than how it achieves it |
| Implementation | Private variables with public getters/setters and validation checks | Abstract classes, pure virtual methods, and interfaces |
| Key Benefit | Maintains internal data integrity and prevents unintended side-effects | Reduces cognitive load and decouples modules via contracts |
| Real-World Analogy | A medical capsule containing powdered medicine inside an outer protective shell | A car steering wheel and accelerator: you drive without knowing carburetor fuel injection math |
Focus
- Encapsulation (Data Hiding)
- HOW data is bundled and protected from corruption
- Abstraction (Complexity Hiding)
- WHAT an entity does rather than how it achieves it
Implementation
- Encapsulation (Data Hiding)
- Private variables with public getters/setters and validation checks
- Abstraction (Complexity Hiding)
- Abstract classes, pure virtual methods, and interfaces
Key Benefit
- Encapsulation (Data Hiding)
- Maintains internal data integrity and prevents unintended side-effects
- Abstraction (Complexity Hiding)
- Reduces cognitive load and decouples modules via contracts
Real-World Analogy
- Encapsulation (Data Hiding)
- A medical capsule containing powdered medicine inside an outer protective shell
- Abstraction (Complexity Hiding)
- A car steering wheel and accelerator: you drive without knowing carburetor fuel injection math
// Abstraction: Interface defines contract
interface PaymentGateway {
void processPayment(double amount);
}
// Encapsulation: Class bundles state with private protection
public class BankAccount implements PaymentGateway {
private String accountNumber;
private double balance; // Data hidden from external mutation
public BankAccount(String accNo, double initialBalance) {
this.accountNumber = accNo;
this.balance = Math.max(0, initialBalance);
}
public double getBalance() { return balance; } // Controlled read
@Override
public void processPayment(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println("Payment of $" + amount + " processed. New balance: $" + balance);
} else {
System.out.println("Transaction declined: Insufficient funds.");
}
}
}Payment of $250.0 processed. New balance: $750.0
2Pillar 3: Inheritance & The Multiple Inheritance Diamond Problem
Inheritanceenables a child subclass () to inherit fields and methods from a parent superclass (), promoting code reuse and establishing an"IS-A" relationship.
While languages like C++ and Python supportMultiple Inheritance(a class inheriting from two classes directly), Java prohibits multiple class inheritance to avoid the notoriousDiamond Problem, resolving it instead through multipleInterfaces.
The Diamond Problem Ambiguity
- 1
Class A (Root Superclass)
Defines method display() { print("A"); }
- 2
Class B & Class C (Subclasses)
Both inherit from A and override display() with distinct logic
- 3
Class D extends B, C
Which version of display() does D inherit? Ambiguity creates compiler collision!
- 4
Java Solution: Interfaces
Interfaces provide contract declarations; default method conflicts require explicit override in D
3Pillar 4: Polymorphism (Compile-time Overloading vs Runtime Overriding)
Definition
Polymorphism — Greek for "many forms": the ability of a single interface or method signature to behave differently based on the underlying object or invocation parameters.
Compile-Time vs Runtime Polymorphism Matrix
| Feature | Compile-Time (Method Overloading / Static Binding) | Runtime (Method Overriding / Dynamic Method Dispatch) |
|---|---|---|
| Mechanism | Multiple methods in the SAME class sharing identical name but different parameters (count, type, order) | Subclass provides a specific implementation of a method already defined in its parent superclass |
| Binding Phase | Resolved during compilation time by javac | Resolved at runtime by JVM using the object’s Virtual Method Table (vtable) |
| Signature Rule | Method name same, parameter list MUST differ; return type alone cannot distinguish | Method name, parameter list, and compatible return type MUST match exactly |
| Keywords | No special keywords required | in Java, in C++ |
| Performance | Faster execution — direct static method call emitted into bytecode | Slight indirection overhead due to runtime vtable pointer lookup |
Mechanism
- Compile-Time (Method Overloading / Static Binding)
- Multiple methods in the SAME class sharing identical name but different parameters (count, type, order)
- Runtime (Method Overriding / Dynamic Method Dispatch)
- Subclass provides a specific implementation of a method already defined in its parent superclass
Binding Phase
- Compile-Time (Method Overloading / Static Binding)
- Resolved during compilation time by javac
- Runtime (Method Overriding / Dynamic Method Dispatch)
- Resolved at runtime by JVM using the object’s Virtual Method Table (vtable)
Signature Rule
- Compile-Time (Method Overloading / Static Binding)
- Method name same, parameter list MUST differ; return type alone cannot distinguish
- Runtime (Method Overriding / Dynamic Method Dispatch)
- Method name, parameter list, and compatible return type MUST match exactly
Keywords
- Compile-Time (Method Overloading / Static Binding)
- No special keywords required
- Runtime (Method Overriding / Dynamic Method Dispatch)
- in Java, in C++
Performance
- Compile-Time (Method Overloading / Static Binding)
- Faster execution — direct static method call emitted into bytecode
- Runtime (Method Overriding / Dynamic Method Dispatch)
- Slight indirection overhead due to runtime vtable pointer lookup
Remember
- Encapsulation bundles data with behavior and hides internal variables via private modifiers and getters/setters.
- Abstraction exposes what an object does while hiding how it works behind interfaces and abstract classes.
- Inheritance establishes IS-A hierarchies; the Diamond Problem is averted in Java by disallowing multiple class inheritance.
- Polymorphism enables flexibility: Overloading is resolved at compile time; Overriding is dispatched at runtime based on the actual instance.
Check yourself
Question 1
In Java, what happens if two methods in the same class have the same name and parameter types, but have different return types (e.g. and )?Question 2
Which OOP concept is primarily responsible for Dynamic Method Dispatch during runtime in Java?