Abstraction

  • Understand abstraction in Python and learn how to use abstract classes for structured programming.

  • What is Abstraction ?

    Abstraction is an Object-Oriented Programming concept that hides internal implementation details and shows only the necessary features to the user.

    👉 Simple meaning:
    What to do is shown, how to do is hidden

    Why Do We Use Abstraction?

    • Hide complex logic

    • Improve security

    • Reduce code complexity

    • Improve readability

    • Make code flexible and scalable

    Real-Life Example

    Car

    • You use accelerator, brake, steering

    • You don’t know internal engine working

    This is Abstraction

    How Abstraction Works in Python

    Python provides abstraction using:

    1. Abstract Classes

    2. Abstract Methods

    We use the abc module:

    • ABC → Abstract Base Class

    • @abstractmethod → Abstract Method

    Syntax of Abstraction

    from abc import ABC, abstractmethod


    class ClassName(ABC):

        

        @abstractmethod

        def method_name(self):

            pass

    Rules:

    • Abstract class cannot be instantiated

    • Child class must implement all abstract methods

    • Abstract methods have no body

Basic Abstraction Example

This example shows how an abstract method is defined in a parent class and implemented in a child class.

from abc import ABC, abstractmethod

class Animal(ABC):

    @abstractmethod
    def sound(self):
        pass

class Dog(Animal):
    def sound(self):
        print("Dog barks")

d = Dog()
d.sound()
  • 2.Abstraction with Multiple Child Classes

Abstraction Using Multiple Child Classes

Different child classes implement the same abstract method differently.

from abc import ABC, abstractmethod

class Shape(ABC):

    @abstractmethod
    def area(self):
        pass

class Rectangle(Shape):
    def area(self):
        print("Area = length × breadth")

class Circle(Shape):
    def area(self):
        print("Area = π × r²")

r = Rectangle()
c = Circle()

r.area()
c.area()
  • 3.Abstraction Using super() 

    Why use super()?

    • Calls parent class constructor

    • Reuses parent class code

    • Avoids duplication

Abstraction with super() Example

This example shows how a child class uses super() to call the abstract parent class constructor.

from abc import ABC, abstractmethod

class Vehicle(ABC):
    def __init__(self, brand):
        self.brand = brand

    @abstractmethod
    def start(self):
        pass

class Car(Vehicle):
    def __init__(self, brand, model):
        super().__init__(brand)   # Calling parent constructor
        self.model = model

    def start(self):
        print(f"{self.brand} {self.model} car starts with a key")

c = Car("Honda", "City")
c.start()
  • 4. Abstraction vs Encapsulation 

    Abstraction

    Encapsulation

    Hides implementation

    Hides data

    Shows only essentials

    Controls access

    Achieved using abstract classes

    Achieved using access modifiers