-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInheritance_Polymorphism.py
More file actions
79 lines (67 loc) · 1.94 KB
/
Inheritance_Polymorphism.py
File metadata and controls
79 lines (67 loc) · 1.94 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
## Oops features Inhertance method
# Class sir made a example
print("-------------------------------------------------")
print("Class Example for Inheritance")
print("-------------------------------------------------")
class Animals:
def __init__(self, name):
self.name = name
def speak(self):
print(f"{self.name} is speaking !!! ")
class dogs(Animals):
def speak(self):
print(f"{self.name} is barking")
Bob = dogs("bob")
Matt = dogs("Matt")
Bob.speak()
Matt.speak()
print("-------------------------------------------------")
print("My exmaple for inheritance")
print("-------------------------------------------------")
## This is my example
# parent class
class Vehicle:
def __init__(self, name, brand):
self.name = name
self.brand = brand
def move(self):
return f"{self.name} Moving around"
class Fourwheeler(Vehicle):
def move(self):
print(f"Hey Bro, Lookout There is a !! {self.brand} {self.name} Going ")
Vehicles1 = Fourwheeler("X7", "BMW")
Vehicles2 = Fourwheeler("Q7", "Audi")
Vehicles1.move()
Vehicles2.move()
## Lets go for polymorphism
print("-------------------------------------------------")
print("class Example for Polymorphism")
print("-------------------------------------------------")
# lets write it then
def make_animal_speak(Animals):
return Animals.speak()
class Cat:
def speak(self):
print("Meow !!")
class Dog:
def speak(self):
print("Bark !!")
cat = Cat()
dog = Dog()
make_animal_speak(cat)
make_animal_speak(dog)
print("-------------------------------------------------")
print("My exmaple for Polymorphism")
print("-------------------------------------------------")
def make_vehicles_move(Vehicles):
return Vehicles.move()
class twowheelers:
def move(self):
print("Hayabusa")
class Suv:
def move(self):
print("XUV500")
bike = twowheelers()
suv = Suv()
make_vehicles_move(bike)
make_vehicles_move(suv)