Professional Documents
Culture Documents
Python Inheritence
Python Inheritence
Python Inheritance
Inheritance allows us to define a class that inherits all the methods and properties from
another class.
Parent class is the class being inherited from, also called base class.
Child class is the class that inherits from another class, also called derived class.
The method of inheriting the properties of parent class into a child class is known as
inheritance.
Code reusability- we do not have to write the same code again and again, we can just
inherit the properties we need in a child class.
Python Inheritance
class Person:
def __init__(self, fname, lname):
Now the Student class has the same properties and methods
as the Person class.
Python Inheritance
Note: The __init__() function is called automatically every time the class is being used to
create a new object.
Example
class Student(Person):
def __init__(self, fname, lname):
#add properties etc.
Python Inheritance
When we add the __init__() function, the child class will no longer inherit the parent's
__init__() function.
The child's __init__() function overrides the inheritance of the parent's __init__()
function.
To keep the inheritance of the parent's __init__() function, add a call to the parent's
__init__() function:
Example
class Student(Person):
def __init__(self, fname, lname):
Person.__init__(self, fname, lname)
Python Inheritance
Python also has a super() function that will make the child class inherit all the methods and
properties from its parent:
Example
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
By using the super() function, you do not have to use the name of the parent element, it will
automatically inherit the methods and properties from its parent.
Add Property
Add a year parameter, and pass the correct year when creating objects:
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
def welcome(self):
If you add a method in the child class with the same name as a function in the parent
class, the inheritance of the parent method will be overridden.
Types Of Inheritance
Depending upon the number of child and parent classes involved, there are four types of inheritance in
python.
Single Inheritance
When a child class inherits only a
single parent class. class Parent:
def func1(self):
print("this is function one")
class Child(Parent):
def func2(self):
print(" this is function 2 ")
ob = Child()
ob.func1()
ob.func2()
Python Multiple Inheritance
ob = Child()
ob1 = Child2()
ob.func1()
ob.func2()
Hybrid Inheritance class Parent:
def func1(self):
This type of inheritance is made up of a
print("this is function
combination of other types of inheritance.
one")
Let’s see an example of hybrid inheritance.
class Child(Parent):
def func2(self):
print("this is function 2")
class Child1(Parent):
def func3(self):
print(" this is function 3"):
ob = Child3()
ob.func1()
Python Method Overriding
ob = Child()
ob.func1()
Method Resolution Order in Python
Every class in Python is derived from the object class. It is the most base type in Python.
So technically, all other classes, either built-in or user-defined, are derived classes and all
objects are instances of the object class.
# Output: True
print(issubclass(list,object))
# Output: True
print(isinstance(5.5,object))
# Output: True
print(isinstance("Hello",object))
Method Resolution Order in Python
In the multiple inheritance scenario, any specified attribute is searched first in the current
class.
If not found, the search continues into parent classes in depth-first, left-right fashion without
searching the same class twice.
This order is also called linearization of MultiDerived class and the set of rules used to find
this order is called Method Resolution Order (MRO).