Python Programming - Classes - Discussion

Discussion Forum : Classes - General Questions (Q.No. 36)
36.
Consider the following Python code:
class Shape:
    def __init__(self, name):
        self.name = name

    def display_info(self):
        print(f"I am a {self.name}")

class Square(Shape):
    def __init__(self, name, side_length):
        super().__init__(name)
        self.side_length = side_length

    def display_info(self):
        super().display_info()
        print(f"My side length is {self.side_length}")

# Create an instance of the Square class
my_square = Square("Square", 4)

# Call the display_info method
my_square.display_info()
What will be the output of the code?
I am a Square \n My side length is 4
My side length is 4 \n I am a Square
I am a Square
My side length is 4
Answer: Option
Explanation:
The code creates an instance of the `Square` class, which overrides the `display_info` method. The output includes messages from both the superclass (`Shape`) and the subclass (`Square`).
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.