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.
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 ratings0% 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.
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()