Abstraction is a key concept in Object-Oriented Programming (OOP) that focuses on hiding the implementation details while exposing only the necessary functionalities. In Python, abstraction is achieved using abstract classes and interfaces.
This tutorial will cover:
abc module and @abstractmethod decoratorAbstraction allows you to define a blueprint for a class without specifying the exact implementation. This ensures that different classes following the blueprint must implement the required methods.
For example, in a payment system, different payment methods like credit cards, UPI, and wallets should have a common structure but different implementations.
Hides unnecessary details from the user
Enforces a contract for subclasses to implement necessary methods
Makes code more maintainable and scalable
An abstract class is a class that cannot be instantiated and contains at least one abstract method. Abstract methods are methods that are declared but not implemented in the base class.
Python provides the abc (Abstract Base Class) module to define abstract classes.
from abc import ABC, abstractmethod
class Vehicle(ABC): # Abstract class
@abstractmethod
def start(self):
pass # No implementation
@abstractmethod
def stop(self):
passHere, Vehicle is an abstract class with two abstract methods: start() and stop(). Any subclass of Vehicle must implement these methods.
A class that inherits from an abstract class must implement all abstract methods; otherwise, it will remain abstract itself.
class Car(Vehicle): # Inheriting from Vehicle
def start(self):
print("Car engine started.")
def stop(self):
print("Car engine stopped.")
class Bike(Vehicle):
def start(self):
print("Bike engine started.")
def stop(self):
print("Bike engine stopped.")
# car = Vehicle() # Error: Abstract classes cannot be instantiated
car = Car()
car.start() # Output: Car engine started
car.stop() # Output: Car engine stopped
bike = Bike()
bike.start() # Output: Bike engine started
bike.stop() # Output: Bike engine stoppedAbstract classes ensure that every subclass provides its own version of start() and stop().
An abstract class can also have regular (concrete) methods that provide some default implementation.
class Bank(ABC):
def show_info(self):
print("This is a bank.")
@abstractmethod
def get_interest_rate(self):
pass
class SBI(Bank):
def get_interest_rate(self):
return 5.5
sbi = SBI()
sbi.show_info() # Output: This is a bank.
print(sbi.get_interest_rate()) # Output: 5.5Here, show_info() is a regular method and get_interest_rate() is an abstract method.
Python does not have a separate interface keyword like Java. However, an interface can be implemented using an abstract class with only abstract methods.
class Payment(ABC): # This acts as an interface
@abstractmethod
def pay(self, amount):
pass
class CreditCardPayment(Payment):
def pay(self, amount):
print(f"Paid {amount} using Credit Card.")
class UPIPayment(Payment):
def pay(self, amount):
print(f"Paid {amount} using UPI.")
payment1 = CreditCardPayment()
payment1.pay(1000) # Output: Paid 1000 using Credit Card.
payment2 = UPIPayment()
payment2.pay(500) # Output: Paid 500 using UPI.Here, Payment acts as an interface, enforcing a pay() method in all subclasses.
| Feature | Abstract Class | Interface |
|---|---|---|
| Can contain concrete methods? | Yes | No |
| Can have attributes? | Yes | No |
| Supports multiple inheritance? | No (Single inheritance) | Yes (Multiple inheritance) |
| Purpose | Used for code reuse and abstraction | Used for defining a strict contract |
Consider an online food delivery system where different types of orders exist.
class Order(ABC):
@abstractmethod
def calculate_total(self):
pass
class DineInOrder(Order):
def calculate_total(self):
print("Total calculated for dine-in order.")
class OnlineOrder(Order):
def calculate_total(self):
print("Total calculated for online order.")
order1 = DineInOrder()
order1.calculate_total() # Output: Total calculated for dine-in order.
order2 = OnlineOrder()
order2.calculate_total() # Output: Total calculated for online order.The Order class defines an abstract method calculate_total(), which must be implemented by DineInOrder and OnlineOrder.
Abstraction hides implementation details and enforces necessary methods in subclasses.
Abstract classes cannot be instantiated and contain at least one abstract method.
The abc module and @abstractmethod decorator are used to create abstract classes.
Interfaces in Python are implemented using abstract classes with only abstract methods.
Abstract classes allow code reuse, while interfaces enforce a strict contract.
Sign in to join the discussion and post comments.
Sign inPython for Web Development
Python for Web Development is a comprehensive tutorial series covering the fundamentals of building web applications using Flask and Django. From setting up a project to working with databases, authentication, REST APIs, and deployment on cloud platforms, this series provides a solid foundation for developing secure and scalable web applications.
Python Basics
Python is a powerful, high-level programming language known for its simplicity and versatility. It is widely used in various fields, including web development, data science, artificial intelligence, automation, and more. This tutorial series is designed to take you from the basics of Python to more advanced topics, ensuring a strong foundation in programming.