CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Software Engineering > The Four Pillars of Object Oriented Programming (OOP)

The Four Pillars of Object Oriented Programming (OOP)

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 3 min read · 0 Comments
Share: in X

Object-Oriented Programming (OOP) is built on four foundational principles that promote modular, reusable, and maintainable code. These pillars are encapsulation, inheritance, polymorphism, and abstraction. In this post, we will learn about them along with examples and their significance.

Table of Contents

  • 1. Encapsulation
  • 2. Inheritance
  • 3. Polymorphism
  • 4. Abstraction
  • How the Pillars Interact
  • Common Misconceptions
  • Real-World Analogy
  • Conclusion

1. Encapsulation

Definition: Bundling data (attributes) and methods (functions) that operate on the data into a single unit (a class), while restricting direct access to some components.
Key Mechanism:

  • Use private variables to hide data.
  • Provide controlled access via public getter/setter methods.

Example:

public class BankAccount {  
    private double balance;  // Encapsulated data  

    public void deposit(double amount) {  
        if (amount > 0) balance += amount;  // Validation in setter  
    }  

    public double getBalance() {  
        return balance;  // Read-only access via getter  
    }  
}  

Why It Matters:

  • Protects data integrity (e.g., preventing negative balances).
  • Simplifies code maintenance by isolating internal changes.

2. Inheritance

Definition: Allowing a class (subclass) to inherit properties and behaviors from another class (superclass).
Key Mechanism:

  • Use extends for class inheritance.
  • Use implements for interface inheritance.

Example:

class Animal {  // Superclass  
    void eat() { System.out.println("Eating..."); }  
}  

class Dog extends Animal {  // Subclass  
    void bark() { System.out.println("Woof!"); }  
}  

// Usage:  
Dog myDog = new Dog();  
myDog.eat();  // Inherited method  
myDog.bark(); // Subclass-specific method  

Why It Matters:

  • Promotes code reuse and hierarchical organization.
  • Enables polymorphism (via method overriding).

3. Polymorphism

Definition: Allowing objects to take multiple forms. Methods can behave differently based on the object’s type.
Types:

  • Compile-time: Method overloading (same name, different parameters).
  • Runtime: Method overriding (same method in subclass).

Example:

class Shape {  
    void draw() { System.out.println("Drawing a shape"); }  
}  

class Circle extends Shape {  
    @Override  
    void draw() { System.out.println("Drawing a circle"); }  // Overriding  
}  

// Runtime Polymorphism:  
Shape shape = new Circle();  
shape.draw();  // Output: "Drawing a circle"  

Why It Matters:

  • Simplifies code by enabling generic interfaces (e.g., List can be ArrayList or LinkedList).
  • Enhances flexibility in method design.

4. Abstraction

Definition: Hiding complex implementation details and exposing only essential features.
Key Mechanisms:

  • Abstract Classes: Partially implemented classes with abstract methods.
  • Interfaces: Fully abstract contracts (prior to Java 8).

Example:

abstract class Vehicle {  
    abstract void start();  // Abstract method (no implementation)  
}  

class Car extends Vehicle {  
    @Override  
    void start() { System.out.println("Car starts with a key"); }  
}  

// Usage:  
Vehicle vehicle = new Car();  
vehicle.start();  

Why It Matters:

  • Reduces complexity by focusing on “what” instead of “how”.
  • Facilitates modular design (e.g., defining APIs via interfaces).

How the Pillars Interact

  • Encapsulation and abstraction work together to hide details and protect data.
  • Inheritance and polymorphism enable code reuse and dynamic behavior.

Common Misconceptions

  • Abstraction ≠ Encapsulation: Abstraction hides complexity; encapsulation bundles and protects data.
  • Polymorphism ≠ Overloading: Overloading is compile-time; overriding is runtime.

Real-World Analogy

  • Encapsulation: A pill capsule (hides medicine inside).
  • Inheritance: A family tree (traits passed down).
  • Polymorphism: A power button (same action, different results for TV vs. laptop).
  • Abstraction: Driving a car (ignores engine mechanics).

Conclusion

Mastering these pillars allows developers to design robust, scalable systems. Encapsulation ensures security, inheritance promotes reuse, polymorphism enables flexibility, and abstraction manages complexity. Together, they form the backbone of effective OOP design.

Related Posts:

  • Encapsulation in Java
  • Inheritance in Java: A Developer’s Guide to Code Reusability
  • Abstraction in Java
  • Polymorphism in Java
  • Difference between OOP and POP
  • Unit Testing and Integration Testing
Tags:oop
Was this article helpful?
← Previous ArticlePEMDAS in Java: Order of Operations Explained with Examples
Next Article →Advanced Java String Techniques: Compact Strings, Interning, and Custom Structures

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap