In this Python oops program, we will create a class with an abstract method in Python. The program demonstrates the abstract class method in Python with the help of the below-given steps.
Abstract method in Python
Steps to solve the program
- The AbstractClass is defined as a subclass of ABC (Abstract Base Class) from the abc module. This makes AbstractClass an abstract class.
- Inside the AbstractClass, we have a method called abstract_method() decorated with @abstractmethod. This decorator marks the method as an abstract method, which means it must be overridden in the concrete subclass.
- We define a concrete class called ConcreteClass that inherits from AbstractClass. Since ConcreteClass is a concrete class, it must provide an implementation for the abstract method abstract_method() defined in the base class.
- In ConcreteClass, we define the abstract_method() and provide an implementation by printing “Implemented abstract method”.
- An object obj of the ConcreteClass is created.
We call the abstract_method() on the obj object. Since ConcreteClass provides an implementation for the abstract method inherited from AbstractClass, the overridden method in ConcreteClass is invoked, and it prints “Implemented abstract method”.
from abc import ABC, abstractmethod
class AbstractClass(ABC):
@abstractmethod
def abstract_method(self):
pass
class ConcreteClass(AbstractClass):
def abstract_method(self):
print("Implemented abstract method")
# Create an object of the concrete class
obj = ConcreteClass()
obj.abstract_method()
Output:
Implemented abstract method
Related Articles
Write a Python Class program to create a class with data hiding.
Python Class Structure for School Management System.
Write a Python Class Structure for Employee Management Application.
Write a Python Class with @property decorator.
Write a Python Class structure with module-level Import.
Create 5 different Python Classes and access them via a single class object.