Four Pillars of Object-Oriented Programming (OOPS)

By | December 16, 2024

Get an overview of four pillars of object-oriented programming (OOP) and see their suitable real life examples.

What is OOP?

Picture a cluttered garage🛠️. Tools are scattered everywhere, various shelves are overflowing with items and you have to move things around just to find a hammer. Of course, we can argue that it’s our garage and we know where everything is. But, other people will face problems when they use this garage to work!🙀

We sometimes share our code with other programmers to work together. Unstructured and hard-to-read code may only work for one person, but not for two (or more) people. One way we can organize our code is to use object-oriented programming (OOP) concept. We organize code by grouping related properties and behaviors into logical units called classes. To take this even further, OOP is built with four pillars — Abstraction, Encapsulation, Inheritance and Polymorphism. These OOPs concepts allow us to create code that is organized, reusable, and easy to understand.

Let’s discuss these four pillars with their suitable examples in real-life 😊.

1. Abstraction: Focus on What matters, but not How

Imagine driving a car 🚗. Car driver presses the accelerator to move. Driver doesn’t need to know how the engine works. Abstraction shows only what is needed and hides implementation details.

// Abstract class
public abstract class Vehicle {
    public abstract void accelerate(); // Focus on action, not details
}

// Concrete class
public class Car extends Vehicle {
    @Override
    public void accelerate() {
        System.out.println("Car is accelerating!");
    }
}

2. Encapsulation: Keep details Private, use getter and setter method

Encapsulations combine data (private variables) and methods (getter and setter) just like a medicine capsule 💊 has various medicines inside it. Let’s bank account balance. The account balance is private. We cannot change it directly. Instead, we can deposit/withdraw and check using secure setter and getter methods.

public class BankAccount {
    private double bal; // 🔒Private data 

    // Getter method
    public double getBalance() {
        return bal;
    }

    // Setter method with logic
    public void depositBalance(double amt) {
        if (amt > 0) {
            bal += amt;
        } else {
            System.out.println("Insufficient balance!");
        }
    }
}

3. Inheritance: Reuse common properties

Inheritance allows a class (known as child class) can use properties of another class (known as parent class) and also can add its own unique properties. Inheritance avoids redundancy in code. For example, a child inherits some properties (like surname 👨‍👦) from their parents with its own unique properties.

// Parent class
class Father {
    String surname = "Upadhyay";
    void showSurname() { System.out.println("Surname: " + surname); }
}

// Child class
class Son extends Father {
    void hobby() { System.out.println("Playing cricket!"); }
}

There are different types of inheritance in OOPs, like: Single, Multiple, Multilevel, Hierarchy and Hybrid Inheritance.

4. Polymorphism: Same action, but different results

Polymorphism means many forms. Polymorphism allows us to use the same names for methods with different parameters and return types within the class. For example, we can define add function for adding two integers and two strings as below –

class Calculator {
    // Add two integers
    int addition(int x, int y) {
        return x + y;
    }

    // Add two strings
    String addition(String x, String y) {
        return x + y;
    }
}

We have two categories of polymorphism in OOPs:

  • Compile-time (static) polymorphism: It is known as method overloading. It belongs to a single class.
  • Run-time (dynamic) polymorphism: It is known as method overriding. It belongs to parent-child class relationship.

Conclusion

The four pillars of Object-Oriented Programming—Encapsulation, Abstraction, Inheritance, and Polymorphism—work together to make code safer, more organized, and easier to maintain.

  • Abstraction focuses on what’s needed and hides implementation details.
  • Encapsulation locks up sensitive data in private spaces.
  • Inheritance allows you to reuse and extend existing properties.
  • Polymorphism allows you to use same name for multiple behaviors.

Just like managing a garage effectively keeps it functional and safe, applying OOP concepts in your programs are clean, extendable, and robust!💡

Assessment

Question: Which OOP pillar limits direct access to object variables?

  • A) Polymorphism
  • B) Inheritance
  • C) Encapsulation
  • D) Abstraction

Answer:

Option (C) Encapsulation is correct.

Author: Mithlesh Upadhyay

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.