- Object-Oriented Programming (OOP) in Python
-
Overview
- Introduction to OOP in Python
- Classes and Objects
- Constructors (__init__) and Destructors
- Inheritance (Single, Multiple, Multilevel)
- Polymorphism and Method Overriding
- Encapsulation and Data Hiding
- Abstract Classes and Interfaces
- Static and Class Methods
- Magic/Dunder Methods (__str__, __repr__)
- Metaclasses in Python
- Method Resolution Order (MRO) in Python
Abstract Classes and Interfaces
Add to BookmarkIntroduction
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:
- What abstraction is and why it is needed
- Abstract classes and methods in Python
- The
abc
module and@abstractmethod
decorator - Difference between abstract classes and interfaces
- Real-world examples of abstraction
1. What is Abstraction?
Abstraction 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.
Key Benefits of Abstraction
Hides unnecessary details from the user
Enforces a contract for subclasses to implement necessary methods
Makes code more maintainable and scalable
2. Abstract Classes in Python
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.
Creating an Abstract 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):
pass
Here, Vehicle
is an abstract class with two abstract methods: start()
and stop()
. Any subclass of Vehicle
must implement these methods.
3. Implementing an Abstract Class
A class that inherits from an abstract class must implement all abstract methods; otherwise, it will remain abstract itself.
Example: Implementing an Abstract Class
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 stopped
Abstract classes ensure that every subclass provides its own version of start()
and stop()
.
4. Abstract Class with Concrete Methods
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.5
Here, show_info()
is a regular method and get_interest_rate()
is an abstract method.
5. Interfaces in Python
Python does not have a separate interface
keyword like Java. However, an interface can be implemented using an abstract class with only abstract methods.
Example: Defining an Interface
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.
6. Difference Between Abstract Classes and Interfaces
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 |
7. Real-World Example: Online Food Ordering System
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
.
Summary
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.
Prepare for Interview
- JavaScript Interview Questions for 0–1 Year Experience
- JavaScript Interview Questions For Fresher
- SQL Interview Questions for 5+ Years Experience
- SQL Interview Questions for 2–5 Years Experience
- SQL Interview Questions for 1–2 Years Experience
- SQL Interview Questions for 0–1 Year Experience
- SQL Interview Questions for Freshers
- Design Patterns in Python
- Dynamic Programming and Recursion in Python
- Trees and Graphs in Python
- Linked Lists, Stacks, and Queues in Python
- Sorting and Searching in Python
- Debugging in Python
- Unit Testing in Python
- Asynchronous Programming in PYthon
Random Blogs
- AI in Marketing & Advertising: The Future of AI-Driven Strategies
- Internet of Things (IoT) & AI – Smart Devices and AI Working Together
- Time Series Analysis on Air Passenger Data
- The Ultimate Guide to Machine Learning (ML) for Beginners
- Quantum AI – The Future of AI Powered by Quantum Computing
- How to Become a Good Data Scientist ?
- AI in Cybersecurity: The Future of Digital Protection
- Google’s Core Update in May 2020: What You Need to Know
- Mastering SQL in 2025: A Complete Roadmap for Beginners
- Python Challenging Programming Exercises Part 1
- Ideas for Content of Every niche on Reader’s Demand during COVID-19
- Datasets for analyze in Tableau
- Extract RGB Color From a Image Using CV2
- Window Functions in SQL – The Ultimate Guide
- Top 10 Blogs of Digital Marketing you Must Follow
Datasets for Machine Learning
- Amazon Product Reviews Dataset
- Ozone Level Detection Dataset
- Bank Transaction Fraud Detection
- YouTube Trending Video Dataset (updated daily)
- Covid-19 Case Surveillance Public Use Dataset
- US Election 2020
- Forest Fires Dataset
- Mobile Robots Dataset
- Safety Helmet Detection
- All Space Missions from 1957
- OSIC Pulmonary Fibrosis Progression Dataset
- Wine Quality Dataset
- Google Audio Dataset
- Iris flower dataset
- Artificial Characters Dataset