🎭 Abstraction

Last Updated: 05 Sept 2025


Abstraction = hiding implementation details and showing only essential features.

Hinglish Tip 🗣: Jab tum car chalate ho → steering ghumao aur car turn hoti hai.Tumhe andar ka engine kaise kaam karta hai uski detail nahi maloom. Yehi abstraction hai.👉 User ko kya karna hai dikhte hai, kaise hoga nahi.


✏ Abstraction in Python

Python me abstraction implement karne ke liye:

  1. Use abc module (Abstract Base Class).
  2. Use ABC as parent class.
  3. Use @abstractmethod decorator to define abstract methods.
    • Must be implemented in child classes.

💡 Example 1.

from abc import ABC, abstractmethod

class Vehicle(ABC):        # Abstract class
    @abstractmethod
    def start(self):       # Abstract method
        pass

    @abstractmethod
    def stop(self):
        pass

class Car(Vehicle):        # Child must implement abstract methods
    def start(self):
        print("Car started")

    def stop(self):
        print("Car stopped")

class Bike(Vehicle):
    def start(self):
        print("Bike started")

    def stop(self):
        print("Bike stopped")

# v = Vehicle()   # ❌ Error: Can't create object of abstract class
c = Car()
c.start()   # Car started
c.stop()    # Car stopped

💡 Example 2.

from abc import ABC, abstractmethod

class Shape(ABC):
    def __init__(self,shape):
        self.shape=shape

    @abstractmethod
    def area(self):
        pass


class Circle(Shape):
    def __init__(self,shape,ra):
        super().__init__(shape)
        self.ra=ra

    def area(self):
        return  3.14*self.ra**2

class Rectangle(Shape):
    def __init__(self,shape,l,b):
        super().__init__(shape)
        self.l=l
        self.b=b

    def area(self):
        return  self.l*self.b


c1=Circle("Circle",10)
print(c1.area())
print(c1.shape)

r1=Rectangle("RECTANGLE",10,20)
print(r1.area())
print(r1.shape)

💡 Quick Practice


|| राम नाम सत्य है ||