Practical 14
Practical 14
Sr. Remark if
Name of Resource Broad Specification Quantity
No. any
1 Computer System Intel i3, 4 GB RAM
For All
2 Operating System Windows/Linux 20
Experiments
3 Development Software Python IDE And VS Code
# Function breathe
def breathe(self):
print("I breathe oxygen.")
# Function feed
def feed(self):
print("I eat food.")
# Child class
class Herbivorous(Animal):
# Function feed (Overriding the parent class method)
def feed(self):
print("I eat only plants. I am vegetarian.")
Out put:
I eat only plants. I am vegetarian. # Calls the overridden feed() method from Herbivorous class
I breathe oxygen. # Calls the inherited breathe() method from Animal class
1. Write a Python program to create a class to print an integer and a character with two
methods having the same name but different sequence of the integer and the character
parameters. For example, if the parameters of the first method are of the form (int n, char
c), then that of the second method will be of the form (char c, int n)
In Python, the multipledispatch module enables defining multiple versions of a method based
on different parameter types.
class Display:
@dispatch(int, str)
def print_value(self, n, c):
print("Integer:", n, "Character:", c)
@dispatch(str, int)
def print_value(self, c, n):
print("Character:", c, "Integer:", n)
# Creating object
obj = Display()
2. Write a Python program to create a class to print the area of a square and a rectangle.
The class has two methods with the same name but different number of parameters. The
method for printing area of rectangle has two parameters which are length and breadth
respectively while the other method for printing area of square has one parameter which is
side of square.
@dispatch(int, int)
def area(self, length, breadth):
print("Area of Rectangle:", length * breadth)
# Creating object
obj = Shape()
# Calling methods
obj.area(5) # Square (side = 5)
obj.area(4, 6) # Rectangle (length = 4, breadth = 6)
3. Write a Python program to create a class 'Degree' having a method 'getDegree' that
prints "I got a degree". It has two subclasses namely 'Undergraduate' and 'Postgraduate'
each having a method with the same name that prints "I am an Undergraduate" and "I am
a Postgraduate" respectively. Call the method by creating an object of each of the three
classes.
class Degree:
def getDegree(self):
print("I got a degree.")
class Undergraduate(Degree):
def getDegree(self):
print("I am an Undergraduate.")
class Postgraduate(Degree):
def getDegree(self):
print("I am a Postgraduate.")