What is Inheritance in Python?
Inheritance is one of the important pillar of Object Oriented Programming(OOPs). It is used to solve real world relations.
In OOPs we write codes in classes. We create several classes and define different attributes and methods inside class.
With the help of inheritance we can access the features(attributes and methods) of one class into another class.
The class which is inherited is called Parent class or base class and the class which inherits the features of base class is called child class or derived class.
Derived class can access all the features(methods and attributes) of base class.
Advantages of Inheritance
The wide range of benefits are associated with inheritance that makes it one of the important concepts in Object Oriented Programmings. Some of the advantages of OOPs are given below:-
1.Real world Relations: Inheritance is inspired by the concept of genetic. Child inherits the features of Parents and Grand Parents. Child also learns some new features in their life. In the same way we can also solve real world relations with the help of inheritance.
2. Reusability of Code: Inheritance provides reusability of code. The features defined in one class can be called in other class entirely or partially. After inheriting features of base class, derived class can also define its unique features.
3. Modularity of Code: We can develop software or write software codes in small – small modules. It enhances the readability of code. Modularity feature makes codes easy to maintain and super easy to reuse and edit.
4. Transitive Nature: If derived class B inherits base class A, then all the subclass of base class can be inherited by derived class. We can use the subclass features in derived class according to the requirement of derived class.
5. Learning and understanding Inheritance is super easy. Basic knowledge of variables and functions is enough to understand inheritance.
6. Inheritance reduces development time and maintenance cost of software models. This makes it widely acceptable and used in almost all software application development.
Inheritance Syntax:
Syntax for writing inheritance code is very simple. There are base class. it’s features are defined in class body. Then there are derived class. Let’s see the syntax of inheritance below: –
class BaseClass:
{body)
class DerivedClass(BaseClass):
{body}
Example of Inheritance
class Parent:
name ="Ray"
age =12
def display(self):
print("my name is",self.name)
print("my age is",self.age)
class Child(Parent):
def show(self):
print("Parent class called")
child_details = Child()
child_details.display()
child_details.show()
Output
my name is Ray my age is 12 Parent class called
Code Explanation: –
In this inheritance example, There are a Parent class and a Child class. name and age is attribute of Parent class. Then Child class has inherited Parent class. Now we can access the attributes and methods of Parent class.
example2:
class Animal: def __init__(self, name): self.name=name def sound(self): print("The animal makes a sound") class Dog(Animal): def __init__(self, name, breed): super().__init__(name) self.breed=breed def sound(self): print("The dog barks")
In this example, Animal is parent class(base class) and Dog is child class(base class) that inherits the features of Animal Class.
super() is used to call the constructor of parent class. It is a python built-in function and always returns an object.
Dog is overriding the sound function
Types of Inheritance
There are 5 types of inheritance in OOPs: –
- Single Inheritance
- Multiple Inheritance
- Multilevel Inheritance
- Hirarchical Inheritance
- Hybrid Inheritance
Single Inheritance
In this, child class inherits features from one parent class. This is most simplest form of inheritance. Let’s see by example: –
class Parent:
def parent_func(self):
print("Parent class is called.")
class Child(Parent):
def child_func(self):
print("child class is called")
child_obj = Child()
child_obj.child_func()
child_obj.parent_func()
Output child class is called Parent class is called.
Code Explanation: –
In this single inheritance example, parent_func() is defined in Parent class. But once child class has inherited parent class we are able to access the Parent class method with the help of Child class object.
Multiple Inheritance
In multiple inheritance, a child class can inherits features from more than one base class. Let’s learn from examples.
class Father:
father_name = "Krish"
def father_func(self):
print("Father class is called.")
class Mother:
mother_name = "Linda"
def mother_func(self):
print("Mother class is called.")
class Child(Father, Mother):
def child_func(self):
print("Father name is: ",self.father_name)
print("mother name is: ",self.mother_name)
print("Child class is called")
child_obj = Child()
child_obj.child_func()
child_obj.father_func()
child_obj.mother_func()
child_obj.father_name = "Peter"
child_obj.mother_name ="Kara"
child_obj.child_func()
Output Father name is: Krish mother name is: Linda Child class is called Father class is called. Mother class is called. Father name is: Peter mother name is: Kara Child class is called
In this code example, Child class has inherited methods and attributes from the both class Father and Mother explained above. We can also assign new values in Parent class attributes with the help of Child class objects if once inherited. It helps a lot of time repeat same code for multiple times.
Multilevel Inheritance
Multilevel inheritance is like GrandParents – Parents – Child relations. Parents inherit attributes and features from GrandParents and Child can inherit features of Parents. Only inheriting Parents class child can access the features of both Parents and GrandParents class.
This is more complex and looks similar like our real life biological blood relations. Let’s learn this also with the help of coding example: –
class GrandFather:
grandFatherName = "Michel"
def grandFatherFunc(self):
print("grand father class is called.")
class Father(GrandFather):
father_name = "Krish"
def father_func(self):
print("Father class is called.")
class Child(Father):
def child_func(self):
print("Grand Father name is: ",self.grandFatherName)
print("father name is: ",self.father_name)
print("Child class is called")
child_obj = Child()
child_obj.child_func()
child_obj.father_func()
child_obj.grandFatherFunc()
Output Grand Father name is: Michel father name is: Krish Child class is called Father class is called. grand father class is called.
In this example, you can see Child Class objects are able to access the features defined in GrandFather Class. we have inherited only Father class in Child Class. Since Father class has also inherited GrandFather Class that’s why child class is also able to access the GrandFather Class features( attributes and methods).
Hirarchical Inheritance
This happens When multiple child classes are created from one parent class. Let’s learn from code example : –
class Parent:
father_name = "Peter"
mother_name ="Linda"
def parent_func(self):
print("Parent class is called.")
class Child1(Parent):
def child_func1(self):
print("child1 class is called")
class Child2(Parent):
def child_func2(self):
print("child2 class is called")
child_obj1 = Child1()
child_obj2 = Child2()
child_obj1.child_func1()
child_obj1.parent_func()
child_obj2.child_func2()
child_obj2.parent_func()
Output child1 class is called Parent class is called. child2 class is called Parent class is called.
Hybrid Inheritance
Hybrid Inheritance is a combination of more than one inheritance. There may be a combination of single inheritance and multiple inheritance or multiple inheritance and multilevel inheritance. Hybrid inheritance complexity increases when number of classes or sub – classes grows.
Inheritance is a versatile feature of OOPs concept.
It helps us to build real life relations in terms of software development or model building.
It is easy to learn and understand. But complexity grows when number of classes or sub classes grows.
It’s better to write a clear and detailed documentation while using python inheritance.