0% found this document useful (0 votes)
11 views2 pages

Lab 12 OOP

The document explains polymorphism and method overriding in Python, emphasizing how child classes can redefine methods inherited from parent classes. It illustrates this concept with examples of class A and class B, demonstrating how overridden methods are called based on the object type. Additionally, it outlines a lab task requiring students to implement a program with a class hierarchy that overrides a lifestyle method for different types of people.

Uploaded by

fiza.j609
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Lab 12 OOP

The document explains polymorphism and method overriding in Python, emphasizing how child classes can redefine methods inherited from parent classes. It illustrates this concept with examples of class A and class B, demonstrating how overridden methods are called based on the object type. Additionally, it outlines a lab task requiring students to implement a program with a class hierarchy that overrides a lifestyle method for different types of people.

Uploaded by

fiza.j609
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Lab 13: Polymorphism and Method Overriding

The word polymorphism means having many forms. Typically, polymorphism occurs
when there is a hierarchy of classes and they are related by inheritance.
Polymorphism means that a call to a member function will cause a different function
to be executed depending on the type of object that invokes the function.
In literal sense, Polymorphism means the ability to take various forms. In Python,
Polymorphism allows us to define methods in the child class with the same name as
defined in their parent class.
As we know, a child class inherits all the methods from the parent class. However,
you will encounter situations where the method inherited from the parent class
doesn't quite fit into the child class. In such cases, you will have to re-implement
method in the child class. This process is known as Method Overriding.
In you have overridden a method in child class, then the version of the method will
be called based upon the type of the object used to call it. If a child class object is
used to call an overridden method then the child class version of the method is
called. On the other hand, if parent class object is used to call an overridden method,
then the parent class version of the method is called.
The following program demonstrates method overriding in action:

class A:
def explore(self):
print("explore() method from class A")
class B(A):
def explore(self):
print("explore() method from class B")

b_obj = B()
a_obj = A()

b_obj.explore()
a_obj.explore()
Output:
explore() method from class B
explore() method from class A
explore() method from class A

Here b_obj is an object of class B (child class), as a result class B version of


the explore() method is called. However, the variable a_obj is an object of
class A (parent class), as a result class A version of the explore()method is called.
If for some reason you still want to access overridden method of the parent class in
the child class, you can call it using the super() function as follows:

python101/Chapter-16/method_overriding_2.py
class A:
def explore(self):
print("explore() method from class A")
class B(A):
def explore(self):
super().explore() # calling the parent class explore()
method
print("explore() method from class B")

b_obj = B()
b_obj.explore()
Output:
explore() method from class A
explore() method from class B

LAB TASKS
You are required to complete the following tasks in the Lab and submit
your solution on LMS in PDF file format.
Task 1: Write a program in which student, businessman and poor will inherit the
method lifestyle from class person. The person’s method will be life style which will
show the life style of following people is following. The method of class student,
businessman and poor will override the method of class person. You can write in
method of student Life style is normal, for businessman is Life style is elite for poor
Life style is not good.

You might also like