Abstraction in Python is used to hide complex details and show only the important features of a program.
In this chapter, you will learn about abstraction in Python, abstract classes, abstract methods, and how to use them with examples.
Abstraction is a concept in object-oriented programming where we hide the internal implementation and show only the required functionality to the user.
In Python, abstraction is achieved using abstract classes and abstract methods with the help of the abc module.
Real Life Example: In a payment system, different payment methods are available such as Credit Card, UPI, and Net Banking. All methods follow a common process, but their internal working is hidden:
Each method works differently - Credit Card uses card details, UPI uses a UPI ID, and Net Banking uses bank login credentials
The system only knows that a payment can be made and verified. How each payment method processes the transaction is hidden from the user.
An abstract base class (ABC) is a class that contains one or more abstract methods and cannot be created directly. These methods do not have any implementation and act as a blueprint for child classes.
To create an abstract base class, we import ABC and abstractmethod from the abc module. The class must inherit from ABC, and abstract methods are defined using the @abstractmethod decorator.
In the following example, we are creating an abstract base class and implementing its methods in a child class.
Output:
Car is starting with a key ignition. Car is stopping using the brake.
Explanation:
In this example, Vehicle is an abstract base class with abstract methods start() and stop(). The Car class inherits from Vehicle and provides implementations for these methods. We cannot create an object of Vehicle, but we can create an object of Car.
In this example, Vehicle is an abstract base class with abstract methods start() and stop(). The Car class inherits from Vehicle and provides implementations for these methods. We cannot create an object of Vehicle, but we can create an object of Car.
In the following example, we are creating an abstract method that must be implemented by a child class.
Explanation:
Here, area() is an abstract method in the Shape class, and it does not contain any implementation.
A concrete method is a normal method that has a complete implementation inside an abstract class. Child classes can use it directly without redefining it.
In the following example, we are using a concrete method inside an abstract class.
Explanation:
The describe() method is a concrete method, so it already has implementation and can be used by child classes.
An abstract property is like an abstract method but used for properties. It is defined using @property along with @abstractmethod, and it must be implemented in the child class.
In the following example, we are defining and implementing an abstract property.
Output:
Samsung
Explanation:
The brand property is abstract in the Device class and is implemented in the Mobile class.
An abstract class cannot be used to create objects directly. We must first create a child class that implements all abstract methods.
In the following example, we are trying to create an object of an abstract class.
Explanation:
This will raise an error because Vehicle is an abstract class and cannot be instantiated directly.
We request you to subscribe our newsletter for upcoming updates.