OOPs

Encapsulation

Encapsulation means keeping the internal data private and exposing only controlled methods (APIs) to read or modify it.

class BankAccount:
    def __init__(self, balance=0):
        self.__balance = balance   # private variable
 
    def deposit(self, amount):
        self.__balance += amount
 
    def withdraw(self, amount):
        if amount <= self.__balance:
            self.__balance -= amount
 
    def get_balance(self):
        return self.__balance
 

Abstraction

Abstraction means exposing only the required functionality and hiding the internal complexity. and inheritance/interfaces help implement abstraction

Polymorphism

Polymorphism allows the same method call to behave differently depending on the underlying object.

// Runtime Polymorphism
Animal a = new Dog();
a.sound();  // bark
 
a = new Cat();
a.sound();  // meow
 

Inheritance

Inheritance allows you to extract common behavior into a parent class so multiple subclasses can share it.