Professional Documents
Culture Documents
Homework
Homework
Code:
class Parent:
self.name = name
def display_info(self):
class Child(Parent):
super().__init__(name)
def display_info(self):
class Main:
def __init__(self):
self.parent = Parent("John")
self.child = Child("Jane")
def display_names(self):
self.parent.display_info()
self.child.display_info()
m = Main()
m.display_names()
First define the Parent class with an __init__ method that sets the name attribute, and a
display_info method that prints out the name of the parent.
Then, we define the Child class that inherits from the Parent class using class Child(Parent)
(Parent). To output the name of the kid instead of the parent, we override the display_info
method.
Lastly, we define the Main class, which in the __init__ function creates an instance of both the
parent and child classes and invokes their display info method using the display names method.