Abstraction | Dataplexa

Abstraction in Python

Abstraction is one of the key principles of Object-Oriented Programming. It allows you to hide unnecessary details and show only what is essential. This makes programs cleaner, easier to maintain, and more user-friendly.

What Is Abstraction?

Abstraction focuses on “what something does” rather than “how it does it.” Users interact with simple methods while the complex logic stays hidden inside the class. This helps reduce confusion and simplifies large applications.

Why Do We Use Abstraction?

It improves security by hiding sensitive logic from the outside world. It reduces complexity because users only deal with the required functions. It allows developers to modify internal implementation without affecting other parts of the program.

How Python Supports Abstraction

Python provides abstraction through the abc (Abstract Base Class) module. A class becomes abstract when it has one or more abstract methods. Abstract classes cannot be instantiated directly — they must be inherited.

Abstract Classes

An abstract class defines a blueprint for other classes. It contains methods that subclasses must implement. This ensures consistency and predictable behavior across related classes.

from abc import ABC, abstractmethod

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

Here, Vehicle is an abstract class, and start() is an abstract method.

Concrete Class

A concrete class is a subclass that provides the actual implementation of abstract methods. Only after implementing all abstract methods can the class create objects. This ensures every subclass behaves as expected.

class Car(Vehicle):
    def start(self):
        print("Car engine started")

Complete Example

Here is a full example showing abstraction in action. Notice how the abstract class hides details, while the concrete class defines behavior. This is a clean design used in many real-world applications.

from abc import ABC, abstractmethod

class PaymentSystem(ABC):

    @abstractmethod
    def make_payment(self, amount):
        pass

class CreditCardPayment(PaymentSystem):

    def make_payment(self, amount):
        print(f"Processing credit card payment of ${amount}")

pay = CreditCardPayment()
pay.make_payment(250)

Benefits of Abstraction

It reduces complexity by allowing users to work with high-level features. It improves security because internal logic remains hidden. It promotes cleaner architecture and better software design.

Real-World Use of Abstraction

Abstraction is used in API design, payment gateways, operating systems, and frameworks. Users interact with simple interfaces while hidden logic handles complex processing. This separation keeps systems efficient and easier to extend.


📝 Practice Exercises


Exercise 1

Create an abstract class Animal with an abstract method sound() and implement two subclasses.

Exercise 2

Create an abstract class Shape with a method area() and implement it in Rectangle and Circle.

Exercise 3

Build an abstract class Device with a method turn_on() and create two concrete classes.

Exercise 4

Create an abstract class Transport with a method move() and implement it in two subclasses.


✅ Practice Answers


Answer 1

from abc import ABC, abstractmethod

class Animal(ABC):
    @abstractmethod
    def sound(self):
        pass

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

class Cat(Animal):
    def sound(self):
        print("Meow")

d = Dog()
d.sound()

Answer 2

from abc import ABC, abstractmethod
import math

class Shape(ABC):
    @abstractmethod
    def area(self):
        pass

class Rectangle(Shape):
    def __init__(self, w, h):
        self.w = w
        self.h = h

    def area(self):
        return self.w * self.h

class Circle(Shape):
    def __init__(self, r):
        self.r = r

    def area(self):
        return math.pi * self.r * self.r

c = Circle(5)
print(c.area())

Answer 3

from abc import ABC, abstractmethod

class Device(ABC):
    @abstractmethod
    def turn_on(self):
        pass

class Laptop(Device):
    def turn_on(self):
        print("Laptop is starting")

class Tablet(Device):
    def turn_on(self):
        print("Tablet is powering on")

l = Laptop()
l.turn_on()

Answer 4

from abc import ABC, abstractmethod

class Transport(ABC):
    @abstractmethod
    def move(self):
        pass

class Car(Transport):
    def move(self):
        print("Car is moving on the road")

class Boat(Transport):
    def move(self):
        print("Boat is moving on water")

t = Car()
t.move()