Introduction
Abstraction is one of the four fundamental pillars of Object-Oriented Programming (OOP), along with Encapsulation, Inheritance, and Polymorphism. It is a powerful concept that helps developers simplify complex systems by hiding unnecessary implementation details and exposing only the essential features to users.
Python provides abstraction primarily through Abstract Classes and the abc (Abstract Base Class) module.
What is Abstraction in Python?
Abstraction is the process of hiding implementation details and showing only the essential functionality to the user.
It focuses on:
- What an object does
- Not how it does it
Example:
When using an ATM:
- You can withdraw money.
- You can check your balance.
- You can deposit money.
However, you do not see:
- Database operations
- Server communication
- Security verification processes
These details are hidden from the user. This is abstraction.
Abstraction vs Encapsulation
| Abstraction | Encapsulation |
|---|---|
| Hides implementation details | Hides data |
| Focuses on what an object does | Focuses on protecting data |
| Achieved using abstract classes | Achieved using private members |
| Improves simplicity | Improves security |
| Design-level concept | Implementation-level concept |
Many beginners confuse abstraction and encapsulation.
Example:
- Abstraction: ATM provides banking services.
- Encapsulation: Account balance is protected from direct access.
Abstract Classes in Python
Python provides abstraction through Abstract Classes.
An abstract class is a class that cannot be instantiated directly and is designed to be inherited by other classes.
Python provides the:
abc
module for creating abstract classes.
Importing the abc Module
from abc import ABC, abstract method
Where:
- ABC stands for Abstract Base Class.
- abstractmethod is used to create abstract methods.
Creating an Abstract Class
Syntax
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass
Explanation:
- Animal is an abstract class.
- sound() is an abstract method.
- Child classes must implement sound().
Abstract Method
An abstract method is a method declared in an abstract class without a complete implementation.
Example:
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass
The method:
sound()
must be implemented by child classes.
Implementing an Abstract Class
class Animal(ABC):
@abstractmethod
def sound(self):
pass
class Dog(Animal):
def sound(self):
print("Dog Barks")
dog = Dog()
dog.sound()
Output:
Explanation:
The Dog class provides the implementation of the abstract method.
What Happens if Abstract Methods Are Not Implemented?
Example:
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass
class Dog(Animal):
pass
dog = Dog()
Output:
Can’t instantiate abstract class Dog
Explanation:
Python forces child classes to implement all abstract methods.
Multiple Abstract Methods
An abstract class can contain multiple abstract methods.
Example:
from abc import ABC, abstractmethod
class Vehicle(ABC):
@abstractmethod
def start(self):
pass
@abstractmethod
def stop(self):
pass
Child Class:
class Car(Vehicle):
def start(self):
print("Car Started")
def stop(self):
print("Car Stopped")
Usage:
car = Car()
car.start()
car.stop()
Output:
Car Stopped
Abstract Class with Normal Methods
Abstract classes can contain both abstract and regular methods.
Example:
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass
def sleep(self):
print("Sleeping")
Child Class:
class Dog(Animal):
def sound(self):
print("Bark")
Usage:
dog = Dog()
dog.sound()
dog.sleep()
Output:
Sleeping
Real-Life Example: Payment System
from abc import ABC, abstractmethod
class Payment(ABC):
@abstractmethod
def pay(self, amount):
pass
Credit Card Payment:
class CreditCard(Payment):
def pay(self, amount):
print(
f"Paid ₹{amount} using Credit Card"
)
UPI Payment:
class UPI(Payment):
def pay(self, amount):
print(
f"Paid ₹{amount} using UPI"
)
Usage:
payment1 = CreditCard()
payment2 = UPI()
payment1.pay(1000)
payment2.pay(500)
Output:
Paid ₹500 using UPI
Explanation:
Users interact with the common pay() method without worrying about implementation details.
Real-Life Example: Employee Management System
from abc import ABC, abstractmethod
class Employee(ABC):
@abstractmethod
def work(self):
pass
Manager:
class Manager(Employee):
def work(self):
print("Managing Team")
Developer:
class Developer(Employee):
def work(self):
print("Writing Code")
Usage:
employees = [
Manager(),
Developer()
]
for employee in employees:
employee.work()
Output:
Writing Code
Each employee performs work differently while following the same structure.
Abstraction and Inheritance
Abstraction is closely related to inheritance.
Example:
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
Child Classes:
class Circle(Shape):
def area(self):
return 3.14 * 5 * 5
class Square(Shape):
def area(self):
return 4 * 4
Each shape calculates area differently while following the same interface.
Advantages of Abstraction
| Advantage | Description |
|---|---|
| Simplicity | Hides complexity |
| Security | Internal details remain hidden |
| Reusability | Common structures can be reused |
| Maintainability | Easier code updates |
| Scalability | Better support for large applications |
Disadvantages of Abstraction
Despite these drawbacks, abstraction is essential for professional software development.
| Disadvantage | Description |
|---|---|
| Additional Complexity | More classes may be required |
| Learning Curve | Beginners may find it difficult |
| Extra Design Effort | Requires planning before implementation |
Common Mistakes
1. Instantiating an Abstract Class
Incorrect:
animal = Animal()
Output:
Abstract classes cannot be instantiated.
2. Forgetting @abstractmethod
Incorrect:
def sound(self):
pass
Without:
@abstractmethod
the method is not abstract.
3. Not Inheriting from ABC
Incorrect:
class Animal:
Correct:
class Animal(ABC):
4. Not Implementing Abstract Methods
Incorrect:
class Dog(Animal):
pass
Python will raise an error when creating an object.
5. Using Abstraction for Small Programs
For very small projects, abstraction may add unnecessary complexity.
Conclusion
Abstraction is a powerful Object-Oriented Programming concept that simplifies complex systems by hiding implementation details and exposing only essential functionality.
In Python, abstraction is achieved using abstract classes and abstract methods provided by the abc module. It helps developers create clean, scalable, secure, and maintainable applications by enforcing a common structure across multiple classes.
Python Abstraction – Interview Questions
Q 1: What is abstraction in Python?
Q 2: Which module supports abstraction?
Q 3: Can abstract classes be instantiated?
Q 4: Why is abstraction used?
Q 5: Can a subclass ignore abstract methods?
Python Abstraction – Objective Questions (MCQs)
Q1. What is abstraction in Python?
Q2. Which module in Python is used to implement abstraction?
Q3. What is an abstract class in Python?
Q4. What will happen if you try to create an object of an abstract class?
Q5. Which decorator is used to declare an abstract method in Python?