0% found this document useful (0 votes)
8 views4 pages

Practical 14

The document is a lab manual for programming with Python, detailing practical exercises and examples related to method overriding and overloading. It includes definitions, comparisons, and sample code for implementing these concepts, as well as examples of class inheritance and method dispatching. Additionally, it provides exercises for creating classes and methods with varying parameters and outputs.
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)
8 views4 pages

Practical 14

The document is a lab manual for programming with Python, detailing practical exercises and examples related to method overriding and overloading. It includes definitions, comparisons, and sample code for implementing these concepts, as well as examples of class inheritance and method dispatching. Additionally, it provides exercises for creating classes and methods with varying parameters and outputs.
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/ 4

Programming with ‘Python’ Practical: 14 Lab Manual (Solved)

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

1. State the difference between method overriding and overloading

Difference Between Method Overriding and Method Overloading


Feature Method Overriding Method Overloading
Redefining a method in the subclass Defining multiple methods in the same
Definition that is already defined in the parent class with the same name but different
class. parameters.
Provides specific implementation Allows multiple ways to call the same
Purpose
in the subclass. method with different arguments.
Number of Requires at least two classes
Happens in a single class.
Classes (inheritance).
Must have the same name and Methods have different numbers or
Parameters
parameters as the parent method. types of parameters.
Can be the same or covariant
Return Type (subtype of return type in the parent Can have any return type.
class).
Usage Used for runtime polymorphism. Used for compile-time polymorphism.
In Python, method overloading is not directly supported but can be achieved using default
arguments or *args.

Jamia Polytechnic Akkalkuwa Page No:01 Prepared by: Sayyed Waliullah


Programming with ‘Python’ Practical: 14 Lab Manual (Solved)

2. What is the output of the following program?


# Parent class
class Animal:
# Properties
multicellular = True
eukaryotic = True

# 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.")

# Creating an object of the child class


herbi = Herbivorous()

# Calling the overridden function feed()


herbi.feed()

# Calling the inherited function breathe()


herbi.breathe()

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

Jamia Polytechnic Akkalkuwa Page No:02 Prepared by: Sayyed Waliullah


Programming with ‘Python’ Practical: 14 Lab Manual (Solved)

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.

from multipledispatch import dispatch

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

# Calling methods with different parameter orders


obj.print_value(10, 'A') # Integer first, character second
obj.print_value('B', 20) # Character first, integer second

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.

from multipledispatch import dispatch


class Shape:
@dispatch(int)
def area(self, side):
print("Area of Square:", side * side)

Jamia Polytechnic Akkalkuwa Page No:03 Prepared by: Sayyed Waliullah


Programming with ‘Python’ Practical: 14 Lab Manual (Solved)

@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.")

# Creating objects of each class


d = Degree()
ug = Undergraduate()
pg = Postgraduate()

# Calling getDegree() method


d.getDegree() # Calls method from Degree class
ug.getDegree() # Calls overridden method from Undergraduate class
pg.getDegree() # Calls overridden method from Postgraduate class

Jamia Polytechnic Akkalkuwa Page No:04 Prepared by: Sayyed Waliullah

You might also like