-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinheritance.py
156 lines (102 loc) · 3.96 KB
/
inheritance.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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#HIERARICHAEL INHERITANCE
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
a = Person("ram", 24)
print("the name of student is",a.name)
print("the age of student is ",a.age)
class student(Person):
def __init__(self,roll):
self.roll = roll
b=student(4)
print("the roll no of student is ",b.roll)
class manager(Person):
def __init__(self,hour):
self.hour=hour
a=manager(7)
print("the working hours of manager is ",a.hour)
'''
there are 5 types of inheritance they are
1. single inheritance
2.multiple inheritance
3.multilevel inheritance
4.hierarichael inheritance
5.hybrid inheritance
1. derived class is inheritated from parent class
2. when two or more than two base classes are inheritated to single derived classes is called multiple inheritance
3.when derived class is inheritated successively one after another from the successive base class is called multilevel
inheritance eg grandfather to father to son
4.when many derived classes are created from one base classes then it is called hierarichael inheritance
5. when multiple inheritance isformed and again multiple inheritance is formed by combining the formed derived classes
and so on is called multiple inheritance
'''
#MULTILEVEL INHERITANCE
# to inherit properties from grandfather to father and father to son we shold invoke the constructor
class grandfather:
def __init__(self,grandfathername):
self.grandfathername=grandfathername
class father(grandfather) :
def __init__(self,grandfathername,fathername):
self.fathername=fathername
grandfather.__init__(self,grandfathername)#invoke
class son(father):
def __init__(self,grandfathername,fathername,sonname):
self.sonname=sonname
father.__init__(self,grandfathername,fathername)#invoke
def print_name(self):
print("the name of grandfather is",self.grandfathername)
print("the name of father is",self.fathername)
print("the name of son is",self.sonname)
a=son("madhav kedar kiran","kedar kiran","kiran")
print(a.grandfathername)
a.print_name()
#MULTIPLE INHERITANCE
class oop:
def __init__(self,mark1):
self.mark=mark1
class dsa:
def __init__(self,mark2):
self.mark=mark2
class student(oop,dsa):
def __init__(self,name,mark1,mark2):
self.name=name
self.mark1=mark1
self.mark2=mark2
a=student("kiran",59,66)
print(a.name)
print(a.mark1)
print(a.mark2)
class Animal:
def __init__(self,breed):
self.breed=breed
def show(self):
print(f"the breed of animal is:{self.breed} " )
class dog(Animal):
def __init__(self,breed,species):
self.species=species
Animal.__init__(self,breed)
def show(self):
Animal.show(self)#overridig a method of classs animal
print(f"the species of dog is:{self.species}")
class cat(dog):
def __init__(self,breed,species,colony):
self.colony=colony
dog.__init__(self,breed,species)
def show(self):
dog.show(self)
print(f"the colony of cat is :{self.colony}")
a=cat("germanstephard","limbre","tonga")
a.show()
# HYBRID INHERITANCE
# when two or more than two types of inheritance are combined then it is called hybrid inheritance.
#syntax
class baseclass:
pass
class derived1(baseclass):
pass
class derived2(baseclass):
pass
class derived3(derived1,derived2):
pass
#here single and multiple inheritance are used