-
Notifications
You must be signed in to change notification settings - Fork 0
/
polymorphism.py
29 lines (26 loc) · 997 Bytes
/
polymorphism.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Polymorphism - One method can have different versions.
# We can override the methods of parent class in the child class.
# The method to be called is decided automatically depending on the object on which the method is invoked.
class Animal:
def eat(self, food):
print("Animal eats food ", food)
def sleep(self, hours):
print("Animal sleep for ", hours, " hours")
class Dog(Animal):
def eat(self, food): # This method is overriden
print("Dog eats food ", food)
# we can also call the parent class method here using super()
# super().eat(food)
def barks(self, sound):
print("Dog is barking ", sound)
class Lion(Animal):
def roars(self, sound):
print("Lion roars ", sound)
dog = Dog()
dog.eat("Chicken") # this invokes the overridden method rather than the parent class method.
dog.sleep(4)
dog.barks("Mediumly")
lion=Lion()
lion.eat("Goat") # this will call the parent class method.
lion.sleep(9)
lion.roars("Loudly")