DEV Community

Khafido Ilzam
Khafido Ilzam

Posted on

OOP (Object-Oriented Programming)

What is OOP?

Think of OOP like organizing things in your life. OOP is how you organize your code (using objects).

Each object has:

  • Properties (data) → things it has
  • Methods (functions) → things it can do

Example:

A Car object

  • "Has: color, brand, speed"
  • "Can do: start(), stop(), accelerate()"

So, instead of having a messy program with all logic mixed together, OOP groups related data and behavior inside objects — making it organized, reusable, and easy to understand.

4 Pillars of OOP (the mindset behind it)

  1. Encapsulation
    Encapsulation is the process of bundling data (properties) and methods (functions) that operate on that data into a single access. Protect internal data and ensure that objects control how their own data is modified.

    • Example: When you drive a car, you don’t need to know how the engine works — just press start.
  2. Abstraction
    Abstraction means showing only relevant details of an object while hiding its internal implementation. Focus on what an object does, not how it does it. Interfaces are a tool to achieve abstraction.

    • Example: Your phone’s “camera app” hides all the complicated photo processing.
  3. Inheritance
    Inheritance allows one class (child/subclass) to inherit properties and methods from another class (parent/superclass), enabling the child class to reuse the code.

    • Example: “SportsCar” can inherit from “Car” but adds more speed.
  4. Polymorphism
    Polymorphism (“many forms”) allows different classes to respond to the same method name in their own unique way. You can call the same method name, but each object does it differently.

    • Example: playSound() could mean barking for a Dog, or meowing for a Cat.

Top comments (0)