We have already explained the OOP concepts of classes and objects. In this tutorial, we will take a deeper dive into the four core pillars of Object-Oriented Programming: Encapsulation, Inheritance, Polymorphism, and Abstraction. These concepts are essential for writing scalable, maintainable, and reusable code in real-world applications.
What is Encapsulation?
Encapsulation is the practice of bundling data (variables) and the methods (functions) that operate on that data into a single unit, known as a class. It restricts direct access to some of an object's components, which prevents accidental modification. To read or update this private data safely, public methods called getters and setters are used.
In one line, it protects data privately and allow access only safe and controlled methods.
Example:
class Bank_data:
def __init__(self, name, account, balance):
self.name = name
self.__account = account
self.__balance = balance
def get_balance(self):
return self.__balance
def get_account(self):
return self.__account
Ac = Bank_data("vijay", 123, 1000)
print(Ac.name)
print(Ac.get_account())
print(Ac.get_balance())
Note: A single underscore (_) indicates that an attribute is meant for internal use. It is only a convention (warning), not true privacy. The attribute can still be accessed directly without getter or setter methods.
What is Inheritance?
Inheritance is an object-oriented programming concept in which one class acquires the attributes (data) and methods (functions) of another class. In Python, a child (derived) class inherits the methods and data members of a parent (base) class, allowing code reuse and easier maintenance.
class Cat:
def LargeCat(self):
print("Big cats like Tiger")
class SmallCat(Cat):
def SmallCat(self):
print("Home cats are small")
c = SmallCat()
c.LargeCat() # Inherited method from Cat
c.SmallCat() # Method of SmallCat
Inhertiance has 5 types: Single, Multiple, Multilevel, Hierarchical, and Hybrid. Above example is single inheritance, where one parent class and one child class, this is the most common one.
Multiple Inheritance
In multiple, one child inherits multiple parent method and data.
Example:
class Father():
def skill_1(self):
print("programming")
class Mother():
def skill_2(self):
print("Cooking")
class Child(Father, Mother):
def skill_3(self):
print("Skilled with Mother and Father Qualities")
B = Child()
B.skill_1()
B.skill_2()
B.skill_3()
Note: The pass statement is a placeholder that tells Python to do nothing. It is commonly used in empty classes, functions, loops, and conditional blocks while the actual code is being written later.
Example:
class Addition():
def add(self):
print("this is addition: + ")
class Others(Addition):
pass
S = Others()
S.add()
Multilevel
Multilevel inheritance is a type of inheritance in which a child class inherits the properties and methods of another child class. In other words, one class inherits from a parent class, and then another class inherits from that derived (child) class.
Example:
- Grandfather → Father → Son
Here:
- Grandfather is the base (parent) class.
- Father inherits from Grandfather.
- Son inherits from Father.
Thus, the Son class indirectly inherits the properties and methods of the Grandfather class through the Father class.
Hierarchical
Hierarchical inheritance is a type of inheritance in which multiple child classes inherit the properties and methods of a single parent (base) class. It forms a tree-like structure, where one parent class has multiple derived classes.
Example:
- Father → ChildA
- Father → ChildB
- Father → ChildC
Here:
- Father is the base (parent) class.
- ChildA, ChildB, and ChildC are derived (child) classes that inherit the properties and methods of the Father class.
Hybrid
Hybrid inheritance is a combination of two or more types of inheritance, such as multiple inheritance and hierarchical inheritance. It combines the features of different inheritance types in a single program.
Example:
Suppose there is a Father class with three children: ChildA, ChildB, and ChildC (hierarchical inheritance). These children are taught by the same Teacher class, and each child inherits features from both the Father and Teacher classes (multiple inheritance). This creates a combination of hierarchical and multiple inheritance, known as hybrid inheritance.
Simple representation:
- Father → ChildA, ChildB, ChildC (Hierarchical)
- Teacher → ChildA, ChildB, ChildC (Multiple)
Since both inheritance types are used together, it is called hybrid inheritance.
Inheritance use in real world
Single Inheritance - High usage (Standard, clean OOP)Method Overriding
class Animal:
def sound(self):
print("Bark")
class Dog(Animal):
def sound(self):
print("Bite")
c = Dog()
c.sound()
What is Polymorphism?
class Dog:
def sound(self):
print(bark)
class Cat:
def sound(self):
print(meow)
c = Cat()
d = Dog()
d.sound()
c.sound()
Both class have same method but different output.
Another Example (In Real world)
class DebitCard:
def pay(self):
print("payment processed via Debit card")
class CreditCard:
def pay(self):
print("payment processed via Credit card")
class UPI:
def pay(self):
print("payment processed via UPI")
class NetBanking:
def pay(self):
print("payment processed via Net Banking")
Payments = [DebitCard(), CreditCard(), UPI(), NetBanking()]
for payment in Payments
payment.pay() Duck typing
class C1:
def nature(self):
print("C1 class object")
class C2:
def nature(self):
print("C2 class object")
def new_nature(obj):
obj.nature()
new_nature(C1())
new_nature(C2())
What is Abstraction?
Abstraction is the process of hiding unnecessary implementation details and showing only the essential features of an object. It allows users to interact with an object without knowing how it works internally.
Example: A shopkeeper sells different varieties of bread in a store. As a customer, you only need to choose and buy the bread. You don't need to know how the bread was baked, what temperature was used, or which ingredients were used. The baking process is hidden from you, while the product (bread) is presented.
class Bakedshop(ABC):
@abstractmethod
def buy_bread(self):
pass
class Bakery(Bakedshop):
def buy_bread(self):
print("Bread is purchased successfully")
shop = Bakery()
shop.buy_bread()
- Hide complexity
- Clean structure
- Simplify large projects
- Enhance security
