Object-Oriented Programming: The Core Concepts
OOP isnβt just theory β itβs the backbone of maintainable software in Java, Python, and many other languages. Here are the four pillars that make OOP so powerful:
𧱠1. Encapsulation
Encapsulation means bundling data and the methods that operate on it into a single unit (class), and restricting direct access to some of the object's components.
// Java
class User {
private String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
# Python
class User:
def __init__(self, name):
self.__name = name # private variable
def get_name(self):
return self.__name
𧬠2. Inheritance
Inheritance lets one class inherit the properties and methods of another. It's the backbone of code reuse.
// Java
class Animal {
void speak() {
System.out.println("I make noise.");
}
}
class Dog extends Animal {
void speak() {
System.out.println("Bark!");
}
}
# Python
class Animal:
def speak(self):
print("I make noise.")
class Dog(Animal):
def speak(self):
print("Bark!")
π 3. Polymorphism
Polymorphism allows methods to behave differently based on the object calling them.
// Java
Animal myDog = new Dog();
myDog.speak(); // Output: Bark!
# Python
animal = Dog()
animal.speak() # Output: Bark!
π§ 4. Abstraction
Abstraction hides complex logic and shows only the relevant details.
// Java
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
}
}
# Python
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def draw(self):
pass
class Circle(Shape):
def draw(self):
print("Drawing Circle")
These four pillars β Encapsulation, Inheritance, Polymorphism, and Abstraction β are essential to writing scalable and reusable code. Once you master OOP, writing maintainable code becomes second nature.
Got questions about OOP or want to see how I use it in real projects? Hit me up!