💡 The 3 Pillars of Object-Oriented Programming

💡 The 3 Pillars of Object-Oriented Programming

👋 Introduction

Have you ever tried to build something big in code, like a web app, a mobile app, or a game, and your logic turned into a messy jungle?

That’s where Object-Oriented Programming (OOP) steps in. It helps you structure your code more like how we understand the real world: with objects.

Imagine describing a dog, a coffee machine, or a bank account. These things have:

  • Properties (what they are)
  • Behaviors (what they do)

OOP brings that same thinking into code.

At the heart of OOP are 3 core ideas. Think of them as 3 strong pillars that support everything:

  • 🧱 Encapsulation
  • 🧬 Inheritance
  • 🔁 Polymorphism

Let’s explore each one using simple words, real-world comparisons, and beginner-friendly code.


🧱 Pillar 1: Encapsulation

Imagine this

You're using a microwave. You press a button, and it heats your food. Do you know how it works inside? Do you need to?

No. And that’s the point.

Encapsulation is like the microwave. It hides the internal wires, sensors, and complexity. It only gives you a simple interface: buttons.

In Code

class Microwave {
  private isOn: boolean = false;

  public start() {
    this.isOn = true;
    this.heat();
  }

  private heat() {
    if (this.isOn) {
      console.log("🍕 Heating your food...");
    }
  }
}

const myMicrowave = new Microwave();
myMicrowave.start(); // 🍕 Heating your food...        

You cannot access the internal method heat() directly. That’s encapsulation.

Why it's helpful

  • Keeps your logic safe from accidental changes
  • You control how people interact with your object
  • Like protecting a car engine so no one can mess with it from the outside

Human version

Think about your body. You don’t consciously manage your heartbeat. It happens automatically. Your body encapsulates that logic. You just go for a run, and your heart reacts on its own. You don’t need to write heart.beat().


🧬 Pillar 2: Inheritance

Imagine this

You’re designing different vehicles: cars, bikes, buses. They all have common behaviors. They can move, stop, and honk.

Would you write the same code three times?

No. You would create a base class like Vehicle, and let other classes inherit from it.

In Code

class Vehicle {
  move() {
    console.log("🚗 Moving forward...");
  }
}

class Car extends Vehicle {
  openTrunk() {
    console.log("🧳 Trunk opened");
  }
}

class Bike extends Vehicle {
  ringBell() {
    console.log("🔔 Ringing bell!");
  }
}

const car = new Car();
car.move();        // 🚗 Moving forward...
car.openTrunk();   // 🧳 Trunk opened

const bike = new Bike();
bike.move();       // 🚗 Moving forward...
bike.ringBell();   // 🔔 Ringing bell!        

Why it's helpful

  • Reuse logic you’ve already written
  • Add new things without repeating yourself
  • Make your code more organized and readable

Human version

Think of a family.

You might say: "My dad is strong and patient. I got that from him." That’s inheritance. You didn’t copy his personality. You inherited some traits and added your own.

Your dad is the parent class. You are the child class.


🔁 Pillar 3: Polymorphism

Imagine this

You click a Play button.

  • On Spotify, it plays a song
  • On YouTube, it plays a video
  • On Netflix, it plays a movie

Same button. Different results.

That’s polymorphism. It means the same method name can behave differently depending on who is using it.

In Code

class Media {
  play() {
    console.log("▶️ Playing media");
  }
}

class Audio extends Media {
  play() {
    console.log("🎵 Playing audio");
  }
}

class Video extends Media {
  play() {
    console.log("🎬 Playing video");
  }
}

function start(media: Media) {
  media.play();
}

start(new Audio()); // 🎵 Playing audio
start(new Video()); // 🎬 Playing video        

You wrote one function called start() and it works for all types of media.

Why it's helpful

  • Keeps your code flexible
  • Works beautifully with shared interfaces or parent classes
  • You can add new types without changing old logic

Human version

Think of the word run:

  • A person runs on a track
  • A machine runs code
  • A company runs a business

Same word, different meaning based on the context. That’s natural polymorphism. Code uses the same idea.


☕ Full Example: Coffee Machines

Let’s bring all 3 pillars together in one real-world system: coffee machines.

Step 1: Encapsulation

class CoffeeMachine {
  private waterLevel: number = 100;

  public makeCoffee() {
    if (this.waterLevel > 0) {
      console.log("☕ Making coffee...");
      this.boilWater();
    } else {
      console.log("❌ Add water first.");
    }
  }

  private boilWater() {
    console.log("💧 Boiling water...");
  }
}        

The method boilWater is hidden. That’s encapsulation.


Step 2: Inheritance

class EspressoMachine extends CoffeeMachine {
  public makeEspresso() {
    console.log("☕ Making espresso shot");
  }
}

class LatteMachine extends CoffeeMachine {
  public makeLatte() {
    console.log("🥛 Making latte with milk");
  }
}        

Both machines inherit the basic coffee-making process. Each adds its own flavor.


Step 3: Polymorphism

function prepareCoffee(machine: CoffeeMachine) {
  machine.makeCoffee();
}

prepareCoffee(new EspressoMachine()); // ☕ Making coffee...
prepareCoffee(new LatteMachine());    // ☕ Making coffee...        

You treat both machines the same, but they can still act differently.


🎯 Final Words

When you build real applications, from e-commerce websites to banking systems, you will deal with complexity. The 3 pillars of OOP help you organize that complexity.

  • 🧱 Encapsulation protects your data and hides inner details
  • 🧬 Inheritance lets you reuse and extend code
  • 🔁 Polymorphism gives you flexibility and cleaner code

Think of them like this

  • You encapsulate your car engine and only interact with the pedals
  • You inherit your family traits and build on them
  • You speak the same words, but people respond based on the situation


✅ Want to remember this?

If you understand these 3 concepts, you are not just writing code. You are thinking like a real developer.

You are making sure:

  • Your code makes sense
  • Others can read and reuse it
  • Future you can extend it without breaking anything

That’s the power of clean, professional, object-oriented code.

NARJIS FATIMA

|Enthusiastic about building intelligent systems that create impact|AI undergraduate | Learning C++|

3w

I am done with oop. But this isn't a refresher, it aslo clarified and solidified my understanding of the core concepts. Davit Gasparyan

Like
Reply
NARJIS FATIMA

|Enthusiastic about building intelligent systems that create impact|AI undergraduate | Learning C++|

3w

I am done with oop. But this article isn't just a refresher; it truly clarified and solidified my understanding of all core concepts🙌🏻 Davit Gasparyan

Like
Reply
NARJIS FATIMA

|Enthusiastic about building intelligent systems that create impact|AI undergraduate | Learning C++|

3w

I am done with oop. But this article isn't just a refresher; it truly clarified and solidified my understanding of all core concepts🙌🏻 Davit Gasparyan

Like
Reply
shahnawaz shaikh

Certified QuickBooks Online & Xero Bookkeeper | B.Com Graduate | Excel Proficient | Seeking US Accounting Role (Remote/Onsite)

1mo

Thanks for sharing, Davit

Like
Reply
Chelike Nikhil

🚀 Full Stack Python Developer | CS Under Graduate '24 | 2x AWS Certified | Red Hat® Certified Enterprise Application Developer | Google Cloud Certified | Oracle Certified | 160K+ LinkedIn Impressions

1mo

Thanks for sharing, Davit

Like
Reply

To view or add a comment, sign in

Others also viewed

Explore topics