-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path19_inheritance_example.py
More file actions
38 lines (30 loc) · 1021 Bytes
/
Copy path19_inheritance_example.py
File metadata and controls
38 lines (30 loc) · 1021 Bytes
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
class Animal:
def __init__(self, color, weight, age):
self.legs = 4
self.color = color
self.weight = weight
self.age = age
def info(self):
print(
"legs =", self.legs,
"color =", self.color,
"weight =", self.weight,
"age =", self.age
)
class Dog (Animal):
def __init__(self, color, weight, age, breed):
Animal.__init__(self, color, weight, age)
self.breed = breed
# polymorphism - overriding (changing the definition of the function after inheriting)
def info(self):
print("breed =", self.breed)
print("This function is changed in the Dog class")
class Cat (Animal):
def __init__(self, color, weight, age, foodhabit):
Animal.__init__(self, color, weight, age)
self.foodhabit = foodhabit
# d = Dog("White", "8 Kgs", "2 Years", "Alsatian")
# c = Cat("Black", "3 Kgs", "3 years", "Mouse")
#
# d.info()
# c.info()