0% found this document useful (0 votes)
5 views3 pages

Unit III File Handling , Classes_Part10

The document discusses method overloading and overriding in Python, highlighting that Python does not support method overloading directly and instead uses default arguments. It provides examples of a Number class demonstrating operator overloading for addition and comparison, as well as a MethodOverloading class using default arguments. Additionally, it explains method overriding through inheritance with a Base and Derived class example.

Uploaded by

Saraswathi
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)
5 views3 pages

Unit III File Handling , Classes_Part10

The document discusses method overloading and overriding in Python, highlighting that Python does not support method overloading directly and instead uses default arguments. It provides examples of a Number class demonstrating operator overloading for addition and comparison, as well as a MethodOverloading class using default arguments. Additionally, it explains method overriding through inheritance with a Base and Derived class example.

Uploaded by

Saraswathi
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/ 3

Method Overloading class Number:

class Number: def __init__(Self,a,b,c):


def __init__(Self,a,b,c): Self.a=a
Self.b=b
Self.a=a Self.c=c
Self.b=b #Predefined syntax
Self.c=c def __add__(Self,other):
N1=Number(1,2,3) a=Self.a+other.a
b=Self.b+other.b
N2=Number(4,5,6) c=Self.c+other.c
res=Number(a,b,c)
N1+N2 (5,7,9) return res
def __lt__(Self,other):
o1=self.a+self.b+self.c
o2=other.a+other.b+other.c
if(o1<o2):
return True
else:
N1=Number(1,2,3) return False
N2=Number(4,5,6)

N3=N1+N2 (5,7,9) #N1 is assigned to self, N2 is for


other
print(N3.a,N3.b,N3.c)
Method Overloading
 Python doesn’t support Method Overloading we have to
use default arguments.
 If we use default arguments in method declaration
class MethodOverloading:
def display(self, a=None, b=None, c=None):
print(a,b,c)
Obj=MethodOverloading()
Obj.display()
Obj.display(10)
Obj.display(10,20)
Obj.display(10,20,30)
Method Overriding
 Is implemented only during Inheritance:

class Base:
def display(Self):
print(“Base Class display”)
class Derived(Base): #if it is not satisfied base class methods definition

def display(Self): #Derived class is redefining the base class method


print(“Derived Class display()”)
obj=Derived()
obj.display()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy