Python Inheritance and Types of Inheritance, OOPS Part-9

Inheritance
Using inheritance you can access another class variable aswellas methods by making them as a parent class, that particular class is known as child or sub class
It will create relationship between one class to another class
app.py
class A:
def __init__(self):
pass
def sqre(self,x):
print(x*x)
class B(A):
def sqrt(self,x):
print(x**0.5)
class C(B):
def cbrt(self,x):
print(x**(1/3))check = B()
check.sqre(5)
check.sqrt(9)
# You couldn't access the children class
# check.cbrt(5)check1 = C()
# You can access the grand-parent class
check1.sqre(5)
check1.sqrt(9)
check1.cbrt(125)
Here you can clearly see that B and C class are children class and A act as parent.While using inheritance you should note that you can access the parent element but you cant access the children element
Result of app.py
25
3.0
25
3.0
4.999999999999999
Here we’re dividing the inheritance by the way it’s working…!
Types of Inheritance
There are 5 types of inheritance
- Single inheritance
- Multiple inheritance
- Multilevel inheritance
- Hybrid inheritance
- Hierarchical inheritance
Single inheritance
One parent and one children element\
app.py
class Father:
def fatherclass(self):
print('father class')
class Children(Father):
def childrenclass(self):
print('children class')check = Children()
check.fatherclass()
check.childrenclass()
Result of app.py
father class
children class
Multiple inheritance
Multiple parent and single children.
app.py
class Father:
def fatherclass(self):
print('father class')
class Mother:
def motherclass(self):
print('mother class')
class Children(Father,Mother):
def childrenclass(self):
print('children class')check = Children()
check.fatherclass()
check.motherclass()
check.childrenclass()
Result of app.py
father class
mother class
children class
Multilevel inheritance
Here the flow will be Grandfather -> Father -> Children
app.py
class Grandfather:
def grandfatherclass(self):
print('grand father class')
class Father(Grandfather):
def fatherclass(self):
print('father class')
class Children(Father):
def childrenclass(self):
print('children class')check = Children()
check.grandfatherclass()
check.fatherclass()
check.childrenclass()
Result of app.py
grand father class
father class
children class
Hierarchical Inheritance
Parent class is derived more than one child class.
app.py
class Father:
def fatherclass(self):
print('father class')
class Children1(Father):
def children1class(self):
print('children 1 class')
class Children2(Father):
def children2class(self):
print('children 2 class')check = Children1()
check.fatherclass()
check.children1class()check1 = Children2()
check1.fatherclass()
check1.children2class()
Result of app.py
father class
children 1 class
father class
children 2 class
Hybrid Inheritance
It is combination of mutliple and multilevel inhertance
app.py
class Daughter:
def daughterclass(self):
print('daughter class')
class Father(Daughter):
def fatherclass(self):
print('father class')
class Mother(Daughter):
def motherclass(self):
print('mother class')
class Relative(Father,Mother):
def relativeclass(self) :
print('relative class')check = Relative()
check.relativeclass()
check.fatherclass()
check.motherclass()
check.daughterclass()
Result of app.py
relative class
father class
mother class
daughter class
OOPS parts
- Class and Object
- Self
- Constructor and Destructor
- New and Call Method
- Magic Methods
- Attributes and Methods
- Super
- Encapsulation
Follow for more interesting topics :-)
Follow us on
Instagram:- https://www.instagram.com/Coder_033/
Youtube:= https://www.youtube.com/channel/UCjTUcwOms1NJDo2UiIvGfyg
Github:- https://github.com/Coder033
Skillshare https://www.skillshare.com/profile/Coder033/26410885