Site icon DataFlair

Python Program on Abstract Class

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

Abstract classes in Python provide a foundational framework for building robust and structured object-oriented programs. Unlike regular classes, abstract classes cannot be instantiated directly, serving as blueprints for derived classes to implement specific methods. This feature facilitates a more disciplined approach to code design, enforcing a blueprint for common functionalities while allowing flexibility for concrete implementations in derived classes.

In this article, we will explore the practical aspects of abstract classes in Python, unraveling their significance in fostering code modularity and reusability and enforcing a clear hierarchy among classes.

Topic Explanation:

Abstract classes offer a key abstraction mechanism in Python, providing a blueprint for common functionalities that must be implemented by derived classes. By defining abstract methods within an abstract class, developers establish a contract that ensures specific behaviors are present in any class inheriting from it. This enforces a consistent structure across related classes, promoting a more organized and modular codebase.

Additionally, abstract classes contribute to the creation of robust class hierarchies, allowing for the implementation of shared functionalities at an abstract level, reducing redundancy and enhancing code maintainability. Through the exploration of abstract classes in Python, developers gain insights into a powerful tool for building extensible and scalable software architectures.

Prerequisite:

Familiarity with the basic concepts of object-oriented programming, including classes, objects, and inheritance.

Knowledge of how to define classes and methods in Python.

Awareness of how inheritance works in Python, including the ability of derived classes to inherit from base classes.

Proficiency in basic Python syntax, including function definitions, variable assignments, and class instantiation.

Understanding of how methods are implemented within classes and how they contribute to the overall functionality of a program.

Code With Comments:

# Importing Abstract Base Class (ABC) and abstractmethod
from abc import ABC, abstractmethod

# Abstract Class Definition
class Button(ABC):
    
    # Abstract Method Declaration
    @abstractmethod
    def click(self):
        pass
    
    # Non-Abstract Method
    def display(self):
        print("This is Non-abstract method")

# Concrete Class Mycolor inheriting from Button
class Mycolor(Button):
    
    # Implementation of Abstract Method
    def click(self):
        print("Color is Red")

# Concrete Class Myphoto inheriting from Button
class Myphoto(Button):
    
    # Implementation of Abstract Method
    def click(self):
        print("This is my photo")

# Instances Creation
M1 = Mycolor()   # Instance of Mycolor (Button 1)
# M2 = Myphoto()   # Instance of Myphoto (Button 2)

# Method Calls on Instances
M1.click()        # Calling the click method for Mycolor
M1.display()      # Calling the display method for Mycolor

# M2.click()      # Calling the click method for Myphoto

Output:
Color is Red.
This is Non-abstract method.

Code Explanation:

Importing Libraries:

Abstract Class Definition:

Abstract Method Declaration:

Non-Abstract Method:

Concrete Class Mycolor:

Implementation of Abstract Method:

Concrete Class Myphoto:

Instances Creation:

Method Calls on Instances:

Conclusion:

In conclusion, the provided Python code exemplifies the use of abstract classes and abstract methods. The abstract class Button outlines a blueprint with an abstract method click and a non-abstract method display. Concrete classes Mycolor and Myphoto inherit from Button and provide specific implementations for the abstract method. The instances M1 and M2 showcase polymorphism, allowing the abstract method to be called on different concrete instances. The output demonstrates that the abstract method click is successfully implemented in the concrete class Mycolor, emphasizing the effectiveness of abstract classes in defining common structures while leaving specific implementations to derived classes.

Exit mobile version