🌟 We hope you're enjoying our content! 🌟

To keep bringing you quality articles, please consider whitelisting our site in your ad blocker. It’s a small step that makes a big difference!

Thank you for your support.

Top Ad unit 728 × 90

Welcome !!! To The World of Programming

OOPS FEATURES

Oops

Abstract Class in Python

 

Abstract Class & Method in Python

Recall, a class in which a method cannot be implemented or a developer cannot provide definition. The object oriented language provide the concept of defining abstract method. The class having an abstract method is to be defined as an abstract class.

Abstract Class in Python



Python. By default do not provide abstract classes. However, Python comes with a module that provides the base for defining Abstract Base classes(ABC). This module name is known as ABC. The Python program needs to import this package or module 'from abc import ABC, abstractmethod'. A method becomes abstract when defined with the keyword @abstractmethod. Now in Python , the two-dimensional figure class is implemented as follows:


from abc import ABC, abstractmethod
# Defining a TwoDFigure class  
class TwoDFigure(ABC): 
     # protected data members
     _d1 = None 
     _d2 = None

     # Defining a Constructor
     def __init__(self,d1,d2):  
        self.d1 = d1
        self.d2 = d2  
    # Defining a abstract method  
     @abstractmethod
     def computeArea(self):  
        pass
        

The abstract class can only be used by inheriting it. However, its object cannot be created i.e. cannot be instantiated. Recall, Inheritance represents a IS_A relationship. Therefore, a Triangle is a two-dimensional figure, a Rectangle is also a two-dimensional figure. The following code demonstrate how to use the abstract class.


# Defining a child class  
class Rectangle(TwoDFigure):      
    # Constructor  
    def __init__(self,length,breadth):  
        TwoDFigure.__init__(self, length,breadth)
       # overriding abstract method  
    def computeArea(self):
        area = self.d1 * self.d2
        print("Area of Rectangle ", area)

# Defining a child class  
class Triangle(TwoDFigure):      
    # Constructor  
    def __init__(self,base,height):  
        TwoDFigure.__init__(self, base,height)
       # overriding abstract method  
    def computeArea(self):
        area = (1.0/2.0) * self.d1 * self.d2
        print("Area of Triangle ", area)
        

Abstract Class  is used to achieve runtime polymorphism. It also defines API (Application Programming Interface). The sub classes implements this API.

The Rectangle and the Triangle class can be tested by the following code is used the test the above defined classes.


# Test  code  
figure = Rectangle(10,20)
figure.computeArea()
figure = Triangle(10,20)
figure.computeArea()

The abstract method is a unimplemented method in an abstract class. It is provided implementation in the respective sub classes. It archives runtime Polymorphism.

Imagine the cut-copy and the paste operation in software. Their implementation varies from one software to another software. 
Abstract Class in Python Reviewed by Syed Hafiz Choudhary on September 16, 2023 Rating: 5

No comments:

Contact Form

Name

Email *

Message *

Powered by Blogger.
!-- JavaScript for Ad Block Detection -->