Site icon DataFlair

Python Program on Constructor Role in Inheritance

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

In the realm of object-oriented programming, inheritance stands as a powerful mechanism, enabling the creation of new classes based on existing ones. Python, known for its simplicity and versatility, employs constructors to facilitate the inheritance process. This article delves into the provided code, shedding light on the role of constructors in inheritance.

Specifically, we explore how a derived class, ‘Marks,’ inherits attributes from its base class, ‘Student,’ and employs a constructor to initialize its unique attributes, showcasing the seamless integration of inheritance and constructors in Python.

Prerequisites

Topic Explanation

The code introduces two classes, ‘Student’ and ‘Marks,’ showcasing the concept of inheritance. The ‘Student’ class acts as the base class, featuring an ‘init’ method that initializes three instance variables: ‘rno’ (roll number), ‘name,’ and ‘course.’ Subsequently, the ‘Marks’ class, which inherits from ‘Student,’ employs its own ‘init’ method. Crucially, it utilizes ‘super().init()’ to invoke the constructor of the base class, ensuring that the inherited attributes are appropriately initialized.

The ‘Marks’ class introduces a new attribute, ‘per’ (percentage), showcasing how a derived class can extend the functionality of its base class. The ‘display’ method in ‘Marks’ then prints both the inherited attributes (‘rno,’ ‘name,’ ‘course’) and the unique attribute (‘per’). The execution of ‘M1 = Marks()’ demonstrates the inheritance chain, where the constructor of the ‘Student’ class is implicitly called through ‘super().init(),’ and the ‘display’ method prints the combined attributes. This exemplifies the synergy between constructors and inheritance in Python, providing a streamlined way to manage attributes across class hierarchies.

Code:

# Role of Constructor in Inheritance
class Student:
    def __init__(self):
        # Constructor of the base class 'Student' initializing instance variables
        self.rno = 100
        self.name = "Vikas Sharma"
        self.course = "B.Tech"

class Marks(Student):
    def __init__(self):
        super().__init__()  # Call the constructor of the base class 'Student'
        self.per = 90  # Additional instance variable specific to the 'Marks' class

    def display(self):
        # Method to display instance variables of both 'Student' and 'Marks' classes
        print(self.per)
        print("------------------")
        print(self.rno)
        print(self.name)
        print(self.course)

# Creating an instance of the 'Marks' class
M1 = Marks()

# Calling the 'display' method to print instance variables of both 'Student' and 'Marks' classes
M1.display()

<div class=”df-code-out”Output:
90
——————
100
Vikas Sharma
B.Tech

Code Explanation:

Summary

In conclusion, the provided code underscores the integral role of constructors in the inheritance paradigm of Python. By inheriting attributes from the ‘Student’ base class and extending functionality in the ‘Marks’ derived class, developers can create efficient and modular code.

The article elucidates the seamless integration of constructors through the ‘super()’ function, emphasizing how it enables the derived class to leverage the constructor of the base class. As Python continues to be a prominent language for OOP, mastering the interplay between constructors and inheritance becomes imperative for crafting scalable and maintainable code.

Exit mobile version