Python Programming - Classes - Discussion

Discussion Forum : Classes - General Questions (Q.No. 32)
32.
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 Circle(Shape):
    def __init__(self, name, radius):
        super().__init__(name)
        self.radius = radius

    def display_info(self):
        super().display_info()
        print(f"My radius is {self.radius}")

# Create an instance of the Circle class
my_circle = Circle("Circle", 5)

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

Post your comments here:

Your comments will be displayed after verification.