Oops Concepts
Oops Concepts
p1 = Person("John", 36)
print(p1.name)
print(p1.age) OUTPUT:- John
36
the self is used as a reference variable which refers to the current class object. It is always the
first argument in the function definition. However, using self is optional in the function call.
Note: The __init__() function is called automatically every time the class is being used to
create a new object.
class Employee:
id = 10;
name = "John"
def display (self):
print("ID: %d \nName: %s"%(self.id,self.name))
emp = Employee()
emp.display()
OUTPUT:- ID: 10
Name: ayush
Polymorphism
person at the same time can have different characteristic. Like a man at the same time is a father,
a husband, an employee. So the same person posses different behavior in different situations.
print(len(“hello"))
OUTPUT:- 5
3
Examples of used defined polymorphic functions :
return x + y+z
print(add(2, 3))
print(add(2, 3, 4))
OUTPUT:- 5
9
Polymorphism with class methods:
class India():
def capital(self):
print("New Delhi is the capital of India.")
def language(self):
print("Hindi the primary language of India.")
def type(self):
print("India is a developing country.")
class USA():
def capital(self):
print("Washington, D.C. is the capital of USA.")
def language(self):
print("English is the primary language of USA.")
def type(self):
print("USA is a developed country.")
obj_ind = India()
obj_usa = USA()
for country in (obj_ind, obj_usa): [obj.ind.capital()]
country.capital() [obj.usa.capital()]
country.language()
country.type()
OUTPUT:- New Delhi is the capital of India.
Hindi the primary language of India.
India is a developing country.
Washington, D.C. is the capital of USA.
English is the primary language of USA.
USA is a developed country.
Polymorphism with Inheritance:
class Bird:
def intro(self):
print("There are many types of birds.")
def flight(self):
print("Most of the birds can fly but some cannot.")
class sparrow(Bird):
def flight(self):
print("Sparrows can fly.")
class ostrich(Bird):
def flight(self):
print("Ostriches cannot fly.")
obj_bird = Bird()
obj_spr = sparrow()
obj_ost = ostrich()
obj_bird.intro()
obj_bird.flight() Output:-There are many types of birds.
obj_spr.intro() Most of the birds can fly but some cannot.
obj_spr.flight() There are many types of birds
Sparrows can fly.
obj_ost.intro() There are many types of birds.
obj_ost.flight() Ostriches cannot fly.
Polymorphism with a Function and objects:
class India():
def capital(self):
print("New Delhi is the capital of India.")
def language(self):
print("Hindi the primary language of India.")
def type(self):
print("India is a developing country.")
class USA():
def capital(self):
print("Washington, D.C. is the capital of USA.")
def language(self):
print("English is the primary language of USA.")
def type(self):
print("USA is a developed country.")
def func(obj):
obj.capital()
obj.language()
obj.type()
obj_ind = India()
obj_usa = USA()
func(obj_ind)
func(obj_usa)
Output:- New Delhi is the capital of India.
Hindi the primary language of India.
India is a developing country.
Washington, D.C. is the capital of USA.
English is the primary language of USA.
USA is a developed country.
Difference between Overloading and Overriding
OVERLOADING OVERRIDING
In Method Overloading, Methods of the same class In Method Overriding, sub class have the same
shares the same name but each method must have method with same name and exactly the same number
different number of parameters or parameters having and type of parameters and same return type as a
different types and order. super class.
Method Overloading is to “add” or “extend” more to Method Overriding is to “Change” existing behavior
method’s behavior. of method.
It is a compile time polymorphism. It is a run time polymorphism.
It may or may not need inheritance in Method It always requires inheritance in Method Overriding.
Overloading.
In Method Overloading, relationship is there between In Method Overriding, relationship is there between
methods of same class. methods of super class and sub class.
Method Overloading
Python does not supports method overloading.
def product(a, b):
p=a*b
print(p)
def product(a, b, c):
p = a * b*c
print(p)
product(4, 5) #THIS WILL GIVE ERROR
product(4, 5, 5)
We may use other implementation in python to make the same function work differently i.e. as
per the arguments.
def add(datatype, *args):
if datatype =='int':
answer = 0
if datatype =='str':
answer =‘’
for x in args:
answer = answer + x
print(answer)
# Integer
add('int', 5, 6) Output:- 11
# String Hi Jhon
add('str', 'Hi ‘, ‘Jhon')
Operator Overloading in Python
Output:- 3
GeeksFor
12
OverOverOverOver
Overriding in Python
# parent class
class Parent:
# child class
class Child(Parent):
Pass
obj2 = Child()
obj2.anything()