Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Homework:

Code:
class Parent:

def __init__(self, name):

self.name = name

def display_info(self):

print("Parent's name is:", self.name)

class Child(Parent):

def __init__(self, name):

super().__init__(name)

def display_info(self):

print("Child's name is:", self.name)

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()

# Instantiate the Main class and test the output

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.

You might also like