Python Inheritance and Types of Inheritance, OOPS Part-9

Narayanan
Towards Dev
Published in
2 min readSep 24, 2022

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

  1. Single inheritance
  2. Multiple inheritance
  3. Multilevel inheritance
  4. Hybrid inheritance
  5. 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

  1. Class and Object
  2. Self
  3. Constructor and Destructor
  4. New and Call Method
  5. Magic Methods
  6. Attributes and Methods
  7. Super
  8. 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

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in Towards Dev

A publication for sharing projects, ideas, codes, and new theories.

Written by Narayanan

Front-end Development | Back-end development | Data Science | Data Visualization | Hodophile | Booklover

No responses yet

Write a response