Python Abstraction Tutorial | Abstract Classes & Abstract Methods with Examples
As software applications become more complex, developers need a way to simplify the user experience while hiding unnecessary implementation details. Abstraction is one of the four fundamental pillars of Object-Oriented Programming (OOP) that allows developers to expose only the essential features of an object while hiding the internal working logic.
Think about using a car—you only need to know how to drive it, not how the engine, transmission, or braking system works internally. Similarly, when using an ATM, you can withdraw money or check your balance without understanding how the bank’s servers process your request.
Python supports abstraction through Abstract Classes and Abstract Methods using the built-in abc (Abstract Base Classes) module. Abstraction improves code organization, simplifies application design, and makes software easier to maintain.
In this lesson, you’ll learn how to create abstract classes, define abstract methods, implement abstraction using the abc module, and build professional applications that separate interface from implementation.
By the end of this lesson, you’ll understand how abstraction helps developers create clean, scalable, and maintainable software architectures.
Learning Objectives
After completing this lesson, you will be able to:
- Understand abstraction in Object-Oriented Programming.
- Differentiate abstraction from encapsulation.
- Create abstract classes.
- Define abstract methods.
- Use Python’s
abcmodule. - Implement abstraction in real-world applications.
- Design flexible and maintainable software.
Topics Covered
- What is Abstraction?
- Why Abstraction is Important
- Abstract Classes
- Abstract Methods
- ABC Module
- Implementing Abstract Classes
- Real-World Applications
- Abstraction vs Encapsulation
- Best Practices
Detailed Lesson Content
Introduction to Abstraction
Imagine you’re using an Online Food Delivery App.
You simply:
- Browse restaurants
- Select food
- Make payment
- Track delivery
You don’t need to know:
- How the payment gateway processes transactions.
- How the restaurant prepares the order.
- How GPS tracking calculates routes.
- How the delivery assignment algorithm works.
You only interact with the features that matter.
This is Abstraction.
Abstraction hides complex implementation details and exposes only the necessary functionality to the user.
What is Abstraction?
Abstraction is the process of hiding implementation details while exposing only the essential features of an object.
It allows developers to focus on what an object does rather than how it does it.
For example:
An ATM allows you to:
- Withdraw Money
- Deposit Money
- Check Balance
The customer never sees the banking software running in the background.
Why Do We Need Abstraction?
Without abstraction:
- Applications become difficult to use.
- Internal logic is exposed.
- Maintenance becomes harder.
- Code becomes tightly coupled.
With abstraction:
- Cleaner code.
- Better security.
- Easier maintenance.
- Flexible software design.
- Simplified user interaction.
Abstract Classes
An Abstract Class cannot be instantiated directly.
It serves as a blueprint for other classes.
Python provides the abc module for creating abstract classes.
Example:
from abc import ABC
class Vehicle(ABC):
pass
The Vehicle class defines the common structure that other vehicle classes will follow.
Abstract Methods
An abstract method is a method that is declared but has no implementation in the abstract class.
Example:
from abc import ABC, abstractmethod
class Vehicle(ABC):
@abstractmethod
def start(self):
pass
Every child class must implement the start() method.
Implementing an Abstract Class
from abc import ABC, abstractmethod
class Vehicle(ABC):
@abstractmethod
def start(self):
pass
class Car(Vehicle):
def start(self):
print("Car Started")
car = Car()
car.start()
Output
Car Started
Attempting to Instantiate an Abstract Class
vehicle = Vehicle()
Output
TypeError:
Can't instantiate abstract class Vehicle
Abstract classes are meant to be inherited, not instantiated.
Multiple Abstract Methods
An abstract class can define multiple abstract methods.
Example:
from abc import ABC, abstractmethod
class Payment(ABC):
@abstractmethod
def pay(self):
pass
@abstractmethod
def refund(self):
pass
Every subclass must implement both methods.
Real-World Example 1: Payment Gateway
from abc import ABC, abstractmethod
class Payment(ABC):
@abstractmethod
def pay(self):
pass
class UPI(Payment):
def pay(self):
print("Payment via UPI")
class CreditCard(Payment):
def pay(self):
print("Payment via Credit Card")
upi = UPI()
upi.pay()
Output
Payment via UPI
Real-World Example 2: Online Learning Platform
from abc import ABC, abstractmethod
class Course(ABC):
@abstractmethod
def start_course(self):
pass
class PythonCourse(Course):
def start_course(self):
print("Python Course Started")
course = PythonCourse()
course.start_course()
Real-World Example 3: Vehicle Management
from abc import ABC, abstractmethod
class Vehicle(ABC):
@abstractmethod
def fuel_type(self):
pass
class PetrolCar(Vehicle):
def fuel_type(self):
print("Petrol")
class ElectricCar(Vehicle):
def fuel_type(self):
print("Electric")
car = ElectricCar()
car.fuel_type()
Real-World Example 4: Employee Portal
from abc import ABC, abstractmethod
class Employee(ABC):
@abstractmethod
def work(self):
pass
class Developer(Employee):
def work(self):
print("Writing Code")
class Designer(Employee):
def work(self):
print("Designing UI")
developer = Developer()
developer.work()
Abstraction vs Encapsulation
| Encapsulation | Abstraction |
|---|---|
| Protects data | Hides implementation |
| Uses private variables | Uses abstract classes |
| Focuses on security | Focuses on simplicity |
| Controls access | Defines required behavior |
Both concepts work together but solve different problems.
Common Mistakes
Instantiating an Abstract Class
Incorrect
vehicle = Vehicle()
Always create an object of a child class instead.
Forgetting to Implement Abstract Methods
Incorrect
class Car(Vehicle):
pass
Python will raise an error because the abstract method is missing.
Not Importing the abc Module
Always import:
from abc import ABC, abstractmethod
before creating abstract classes.
Best Practices
- Use abstraction for common interfaces.
- Keep abstract classes focused on shared behavior.
- Implement all abstract methods in child classes.
- Avoid putting unnecessary code inside abstract classes.
- Combine abstraction with inheritance for scalable designs.
- Use meaningful class and method names.
Key Takeaways
- Abstraction hides implementation details.
- It exposes only essential functionality.
- Python supports abstraction using the
abcmodule. - Abstract classes cannot be instantiated.
- Child classes must implement all abstract methods.
- Abstraction improves code maintainability, scalability, and readability.
- It is widely used in enterprise software, frameworks, and APIs.



