Introduction
Welcome to my 61st blog post on the Python journey. On day 61, I learned about an OOPs concept called 'INHERITANCE' in Python. It allows us to derive one class from another class. Let's dive into more details and understand Inheritance in Python.
So let's get started......
What is inheritance?
A class derives from another class.
The child class that is derived inherits all the public and protected properties and methods from the parent class.
In addition, the child class can have its own properties and methods, this is called as inheritance.
Inheritance enables the re-usability of code.
Syntax -
class BaseClass:
Body of base class
class DerivedClass(BaseClass):
Body of derived class
Types of Inheritance
Single inheritance
Multiple inheritance
Multilevel inheritance
Hierarchical Inheritance
Hybrid Inheritance
Single Inheritance
Here the derived class inherits properties from a single parent class, thus enabling code reusability.
Example -
class Parent:
def func1(self):
print("This function is in parent class.")
class Child(Parent):
def func2(self):
print("This function is in child class.")
object = Child()
object.func1()
object.func2()
Multiple Inheritance
Here the class can inherit behaviour from more than one base class. This enables the child class to have properties of all the base classes.
Example -
class Mother:
mothername = ""
def mother(self):
print(self.mothername)
class Father:
fathername = ""
def father(self):
print(self.fathername)
class Son(Mother, Father):
def parents(self):
print("Father name is :", self.fathername)
print("Mother :", self.mothername)
s1 = Son()
s1.fathername = "Mommy"
s1.mothername = "Daddy"
s1.parents()
Multilevel Inheritance
Here the features of the base class and the derived class are further inherited into the new derived class.
This relationship is similar to that of a child and his grandfather.
Example -
class Grandfather:
def __init__(self, grandfathername):
self.grandfathername = grandfathername
class Father(Grandfather):
def __init__(self, fathername, grandfathername):
self.fathername = fathername
Grandfather.__init__(self, grandfathername)
class Son(Father):
def __init__(self, sonname, fathername, grandfathername):
self.sonname = sonname
Father.__init__(self, fathername, grandfathername)
def print_name(self):
print('Grandfather name :', self.grandfathername)
print("Father name :", self.fathername)
print("Son name :", self.sonname)
s1 = Son('Prince', 'Rampal', 'Lal mani')
print(s1.grandfathername)
s1.print_name()
Hierarchical Inheritance
In this case, more than one child class are derived from a single parent class.
Example -
class Parent:
def func1(self):
print("This function is in parent class.")
class Child1(Parent):
def func2(self):
print("This function is in child 1.")
class Child2(Parent):
def func3(self):
print("This function is in child 2.")
object1 = Child1()
object2 = Child2()
object1.func1()
object1.func2()
object2.func1()
object2.func3()
Hybrid Inheritance
Inheritance consisting of multiple types of inheritance is called hybrid inheritance.
Example -
class School:
def func1(self):
print("This function is in school.")
class Student1(School):
def func2(self):
print("This function is in student 1. ")
class Student2(School):
def func3(self):
print("This function is in student 2.")
class Student3(Student1, School):
def func4(self):
print("This function is in student 3.")
object = Student3()
object.func1()
object.func2()
Resources Used
You can watch the video of Day#61 by clicking on the below link ๐๐๐๐๐
Conclusion
Thanks, guys for going through this blog post. On day 61, I learned about the concept of Inheritance in Python and its various types.
Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of Python coding. This is it for Day #61
See you in the next one.....
About Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.