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:

  • Object-Oriented Programming (OOP) Understanding:

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

  • Python Class and Method Concepts:

Knowledge of how to define classes and methods in Python.

  • Understanding of Inheritance:

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

  • Basic Python Syntax:

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

  • Implementation of Methods:

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:

  • from abc import ABC, abstractmethod imports the Abstract Base Class (ABC) and the abstract method decorator.

Abstract Class Definition:

  • class Button(ABC): defines an abstract class named Button, inheriting from ABC.

Abstract Method Declaration:

  • @abstractmethod decorator marks the click method as abstract, requiring concrete implementations in derived classes.

Non-Abstract Method:

  • def display(self): defines a non-abstract method within the abstract class.

Concrete Class Mycolor:

  • class Mycolor(Button): defines a concrete class Mycolor that inherits from the abstract class Button.

Implementation of Abstract Method:

  • def click(self): in Mycolor provides the concrete implementation of the abstract click method.

Concrete Class Myphoto:

  • class Myphoto(Button): defines another concrete class Myphoto inheriting from Button.

Instances Creation:

  • M1 = Mycolor() creates an instance M1 of Mycolor (Button 1).
  • M2 = Myphoto() creates an instance M2 of Myphoto (Button 2)

Method Calls on Instances:

  • M1.click() calls the concrete click method for Mycolor.
  • M1.display() calls the non-abstract display method for Mycolor.
  • M2.click() would call the click method for Myphoto.

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.

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *