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: Subject, Target Audience, Topic Core Full size

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).

  1. 1Subject — Computer Science
  2. 2Target Audience — BCA, BTech & MCA Students
  3. 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(private \text{private}\ , protected \text{protected}\ ).

Abstractionhides internal implementation complexity and exposes only the essential interface to the outside world, realized throughAbstract ClassesandInterfaces.

Core Distinction: Encapsulation vs Abstraction

  • 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
java· Java: Encapsulation and Abstraction in Action
// 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.");
        }
    }
}
Output:
Payment of $250.0 processed. New balance: $750.0

2Pillar 3: Inheritance & The Multiple Inheritance Diamond Problem

Inheritanceenables a child subclass (derived \text{derived}\ ) to inherit fields and methods from a parent superclass (base \text{base}\ ), 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. 1

    Class A (Root Superclass)

    Defines method display() { print("A"); }

  2. 2

    Class B & Class C (Subclasses)

    Both inherit from A and override display() with distinct logic

  3. 3

    Class D extends B, C

    Which version of display() does D inherit? Ambiguity creates compiler collision!

  4. 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

  • 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)
    @Override@Override in Java, virtual \text{virtual}\ 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
Exam tip: In Python, runtime polymorphism is naturally embodied byDuck Typing: "If it walks like a duck and quacks like a duck, it is a duck." Python does not support classic C++ style compile-time method overloading directly (the latest definition in code replaces prior definitions with the same name).

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

  1. 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. int add(int ,int )\text{int}\ add(\text{int}\ , \text{int}\ ) and double add(int ,int )\text{double}\ add(\text{int}\ , \text{int}\ ))?
  2. Question 2

    Which OOP concept is primarily responsible for Dynamic Method Dispatch during runtime in Java?

Read it? Now test it. Timed exam questions on this topic, then a report on what to revise.

Take a test

Want a lesson on another topic? Type any topic or add a photo of your notes — get a lesson like this in seconds.

Learn a topic

More Computer Science explained