15. Problem to show abstract method using Python class

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
  1. The AbstractClass is defined as a subclass of ABC (Abstract Base Class) from the abc module. This makes AbstractClass an abstract class.
  2. 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.
  3. 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.
  4. In ConcreteClass, we define the abstract_method() and provide an implementation by printing “Implemented abstract method”.
  5. 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

Python Class with Method Overriding.

Python Class program to create a class with data hiding.

Leave a Comment