Abstraction in Python is an object-oriented programming concept used to hide unnecessary implementation details and show only the essential features of an object. It helps programmers focus on what an object does instead of how it performs a particular task.
Abstraction makes programs easier to understand, maintain, and modify by hiding complex internal operations. In Python, abstraction can be implemented using abstract classes and abstract methods provided by the abc module. For example, a payment system may provide a common pay() method while hiding the different implementation details of credit card, UPI, or bank payments.
Table of Contents
What is Abstraction in Python?
Abstraction is the process of hiding unnecessary implementation details and exposing only the essential functionality to the user. It allows programmers to work with an object through a simple interface without needing to understand its internal implementation.
In Python, abstraction is commonly implemented using abstract classes and abstract methods from the abc module.
Example:
# Python program to understand abstraction
from abc import ABC, abstractmethod
class Vehicle(ABC):
@abstractmethod
def start(self):
pass
class Car(Vehicle):
def start(self):
print("Car starts with a key")
car = Car()
car.start()
Output:
Car starts with a key
Explanation:
- The Vehicle class inherits from ABC, making it an abstract class.
- The start() method is declared as an abstract method.
- The Car class inherits from Vehicle.
- The Car class provides the implementation of the start() method.
- The user can call start() without knowing the internal implementation.
Why Use Abstraction in Python?
Abstraction is useful when you want to hide complex implementation details and provide a simple interface to users. It is commonly used in large applications where different classes need to follow the same structure.
- Hides implementation details: Abstraction hides complex internal operations and exposes only the necessary functionality.
- Improves code readability: By showing only essential features, abstraction makes programs easier to understand.
- Provides a common interface: Abstract classes can define methods that child classes must implement.
- Improves code maintainability: Changes can be made to the internal implementation without changing how the object is used.
- Supports flexible program design: Different child classes can provide their own implementations of the same abstract methods.

How Abstraction Works in Python?
Abstraction in Python works by defining a common structure in an abstract class and requiring child classes to provide the implementation of abstract methods. The abc module provides the ABC class and the @abstractmethod decorator for creating abstract classes and methods.
Example:
# Python program to understand how abstraction works
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass
class Dog(Animal):
def sound(self):
print("Dog barks")
dog = Dog()
dog.sound()
Output:
Dog barks
Explanation:
- The Animal class inherits from ABC.
- The sound() method is marked with @abstractmethod.
- The Dog class inherits from Animal.
- The Dog class provides the implementation of the sound() method.
- The Dog object can call sound() using the implementation provided by the child class.
Abstract Classes in Python
An abstract class is a class that is designed to be inherited by other classes. It can contain abstract methods that require concrete subclasses to provide implementations. Python provides the ABC class in the abc module to create abstract classes.
Syntax:
from abc import ABC, abstractmethod
class ClassName(ABC):
@abstractmethod
def method_name(self):
pass
Explanation:
- ABC: A base class used to create abstract classes.
- abstractmethod: A decorator used to declare an abstract method.
- ClassName: The name of the abstract class.
- method_name(): The abstract method that child classes are expected to implement.
- pass: Indicates that no concrete implementation is provided in this example.
Example:
# Python program to create an abstract class
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
class Circle(Shape):
def area(self):
print("Calculating area of circle")
circle = Circle()
circle.area()
Output:
Calculating area of circle
Explanation:
- Shape is created as an abstract class by inheriting from ABC.
- The area() method is declared as an abstract method.
- Circle inherits from Shape.
- The Circle class implements the area() method.
- An object of Circle can be created because the abstract method has been implemented.
Abstract Methods in Python
An abstract method is a method that is declared in an abstract class and requires concrete subclasses to provide an implementation. The @abstractmethod decorator is used to declare an abstract method.
Syntax:
@abstractmethod
def method_name(self):
pass
Example:
# Python program to demonstrate an abstract method
from abc import ABC, abstractmethod
class Employee(ABC):
@abstractmethod
def calculate_salary(self):
pass
class Developer(Employee):
def calculate_salary(self):
print("Salary calculated for Developer")
developer = Developer()
developer.calculate_salary()
Output:
Salary calculated for Developer
Explanation:
- Employee is an abstract class.
- calculate_salary() is declared as an abstract method.
- Developer inherits from Employee.
- Developer implements calculate_salary().
- The method can be called using the Developer object.
Note:
If a child class does not implement all inherited abstract methods, it also remains abstract and cannot be instantiated.
Creating an Abstract Class Using ABC
The ABC class provides the basic structure required for creating abstract classes. A class that inherits from ABC can contain one or more abstract methods.
Example:
# Python program to create an abstract class using ABC
from abc import ABC, abstractmethod
class Bank(ABC):
@abstractmethod
def withdraw(self):
pass
class SBI(Bank):
def withdraw(self):
print("Withdrawal from SBI account")
account = SBI()
account.withdraw()
Output:
Withdrawal from SBI account
Explanation:
- The Bank class inherits from ABC.
- The withdraw() method is declared using @abstractmethod.
- The SBI class inherits from Bank.
- The SBI class implements the withdraw() method.
- An object of SBI is created, and the method is called.
Implementing Abstract Methods in Child Classes
Child classes must implement inherited abstract methods before their objects can be created. This allows an abstract class to define a common interface while allowing each child class to provide its own implementation.
Example:
# Python program to implement an abstract method in a child class
from abc import ABC, abstractmethod
class Payment(ABC):
@abstractmethod
def pay(self):
pass
class UPI(Payment):
def pay(self):
print("Payment made using UPI")
class Card(Payment):
def pay(self):
print("Payment made using Card")
upi = UPI()
card = Card()
upi.pay()
card.pay()
Output:
Payment made using UPI
Payment made using Card
Explanation:
- Payment defines the abstract pay() method.
- UPI inherits from Payment and implements pay().
- Card also inherits from Payment and provides its own implementation.
- Both child classes follow the same interface.
- The implementation of pay() can be different for each payment method.
Abstraction Using Multiple Abstract Methods
An abstract class can contain more than one abstract method. Child classes must provide implementations for all inherited abstract methods before they can be instantiated.
Example:
# Python program to demonstrate multiple abstract methods
from abc import ABC, abstractmethod
class Computer(ABC):
@abstractmethod
def start(self):
pass
@abstractmethod
def shutdown(self):
pass
class Laptop(Computer):
def start(self):
print("Laptop started")
def shutdown(self):
print("Laptop shut down")
laptop = Laptop()
laptop.start()
laptop.shutdown()
Output:
Laptop started
Laptop shut down
Explanation:
- The Computer class is an abstract class.
- It contains two abstract methods: start() and shutdown().
- The Laptop class inherits from Computer.
- The Laptop class implements both abstract methods.
- The object can be created because all required abstract methods are implemented.
Abstraction with Inheritance
Abstraction is commonly used together with inheritance. An abstract parent class defines the common structure, while child classes provide their own implementations of the abstract methods.
Example:
# Python program to demonstrate abstraction with inheritance
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass
class Dog(Animal):
def sound(self):
print("Dog makes a barking sound")
class Cat(Animal):
def sound(self):
print("Cat makes a meowing sound")
dog = Dog()
cat = Cat()
dog.sound()
cat.sound()
Output:
Dog makes a barking sound
Cat makes a meowing sound
Explanation:
- Animal defines the common abstract sound() method.
- Dog inherits from Animal and provides its implementation.
- Cat also inherits from Animal and provides its own implementation.
- Both classes follow the same interface.
- The actual behavior depends on the child class.
Advantages of Abstraction
Abstraction provides several advantages when designing object-oriented Python programs.
- Hides unnecessary details: Abstraction hides complex implementation details and shows only the important functionality.
- Improves code organization: Abstract classes provide a clear structure that related classes can follow.
- Provides a common interface: Different child classes can implement the same abstract methods in different ways.
- Makes code easier to maintain: Internal implementation can be changed without affecting the way objects are used.
- Supports reusable designs: Abstract classes can be reused as a common base for multiple related classes.
Common Beginner Mistakes
1. Trying to Create an Object of an Abstract Class: An abstract class containing unimplemented abstract methods cannot be instantiated.
Wrong:
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass
animal = Animal()
Correct:
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass
class Dog(Animal):
def sound(self):
print("Dog barks")
dog = Dog()
dog.sound()
2. Forgetting to Use @abstractmethod: Simply defining a method inside a class does not make it abstract. The @abstractmethod decorator is used to declare an abstract method.
Wrong:
from abc import ABC
class Animal(ABC):
def sound(self):
pass
Correct:
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass
3. Not Implementing All Abstract Methods: A child class must implement all inherited abstract methods before an object can be created.
Wrong:
from abc import ABC, abstractmethod
class Vehicle(ABC):
@abstractmethod
def start(self):
pass
@abstractmethod
def stop(self):
pass
class Car(Vehicle):
def start(self):
print("Car started")
car = Car()
Correct:
from abc import ABC, abstractmethod
class Vehicle(ABC):
@abstractmethod
def start(self):
pass
@abstractmethod
def stop(self):
pass
class Car(Vehicle):
def start(self):
print("Car started")
def stop(self):
print("Car stopped")
car = Car()
car.start()
car.stop()
4. Forgetting to Inherit from ABC: Using @abstractmethod is intended to work with Python's abstract base class mechanism. The class should inherit from ABC or use the corresponding abstract base class mechanism correctly.
Wrong:
from abc import abstractmethod
class Vehicle:
@abstractmethod
def start(self):
pass
Correct:
from abc import ABC, abstractmethod
class Vehicle(ABC):
@abstractmethod
def start(self):
pass
5. Thinking Abstraction Means Complete Data Hiding: Abstraction focuses on hiding unnecessary implementation details and exposing essential functionality. It does not mean that all data or methods become completely inaccessible.
Example:
from abc import ABC, abstractmethod
class Printer(ABC):
@abstractmethod
def print_document(self):
pass
class HPPrinter(Printer):
def print_document(self):
print("Printing document")
printer = HPPrinter()
printer.print_document()
Correct Understanding:
The user only needs to know how to call print_document(). The internal printing process can remain hidden inside the implementation.
Conclusion
Abstraction is one of the important concepts of object-oriented programming in Python. It allows you to hide unnecessary implementation details and expose only the essential functionality of an object.
By using abstract classes and abstract methods, you can define a common structure that child classes must follow while allowing each child class to provide its own implementation. Abstraction can be used with inheritance and other OOP concepts to design cleaner, more organized, and maintainable applications.
Frequently Asked Questions (FAQs)
1. What is abstraction in Python?
Abstraction in Python is the process of hiding unnecessary implementation details and exposing only the essential functionality of an object.
2. How is abstraction implemented in Python?
Abstraction is commonly implemented using abstract classes and abstract methods provided by the abc module.
3. What is an abstract class in Python?
An abstract class is a class designed to be inherited by other classes and can contain abstract methods that child classes are expected to implement.
4. What is an abstract method in Python?
An abstract method is a method declared using the @abstractmethod decorator that requires concrete subclasses to provide an implementation.
5. Can we create an object of an abstract class?
No. An abstract class with unimplemented abstract methods cannot be instantiated. A concrete child class must implement the required abstract methods before its object can be created.
0 Comments