Inheritance in Python programming | Hands on Coding

Inheritance

1.The process of getting property of one class into another class is called Inheritance.
2.In other word we can say that the process of deriving a new class from an old class is called inheritance in which the new class is called derived or child or sub class and old class is called Base or Parent or Super class.
3.When a class inherits the property of a class it means it can access all the data member and meber function of that class except private element.
4.In this type of programming mainly two types of classes are used.

  • Parent/Super/Base class
  • Child/Sub/Derived class

Parent/Super/Base class
The class which is inherited by another class is called Parent or Super or Base class.
Child/Sub/Derived class
The class which inherits the property of another class is called Child or Sub or Derived class.

How to inherit one class into another

Derived class (Base class)
Example
class Square(Rectangle)
Here Square is a Derived class and Rectangle is a Base class.

Example


#creating class
class Rectangle:
    #defining function
    def rec_area(self,height,width):
        area=height*width
        print("Area of Rectangle:",area)
#Inheriting Rectangle into Square
class Square(Rectangle):
    # defining function
    def squ_area(self, side):
        area = side*side
        print("Area of Square:", area)
#creating object derived class
obj=Square()
#calling base class function
obj.rec_area(10,20)
#calling derived class function
obj.squ_area(12)
"""
***Output***
Area of Rectangle: 200
Area of Square: 144
"""

Here class Rectangle is a base class and Square is a derived class because Rectangle is inherited into Square therefore we can call all the function using object of Square.

Single Inheritance

In this types of inheritance only two classes are used in which one is inherited by another.


 

#creating class
class Rectangle:
    #defining function
    def rec_area(self,height,width):
        area=height*width
        print("Area of Rectangle:",area)
#Inheriting Rectangle into Square
class Square(Rectangle):
    # defining function
    def squ_area(self, side):
        area = side*side
        print("Area of Square:", area)
#creating object derived class
obj=Square()
#calling base class function
obj.rec_area(10,20)
#calling derived class function
obj.squ_area(12)
"""
***Output***
Area of Rectangle: 200
Area of Square: 144
"""
 

In the above example you can see that there is only two classes are used in which Rectangle is inherited by Square therefore using object of Square we can call function rec_area() and squ_area().

Multiple Inheritance

1.When two or more than two classes are inherited by a single class simultaneously called multiple inheritance.
2.In other word we can say that in this type of inheritance Base class may be two or more than two but derived class should be one.
3.In this type of inheritance atleast three class are compulsory.


 

#creating class
class Rectangle:
    #defining function
    def rec_area(self,height,width):
        area=height*width
        print("Area of Rectangle:",area)
#create class
class Square:
    # defining function
    def squ_area(self, side):
        area = side*side
        print("Area of Square:", area)
# Inheriting Rectangle and Square into Triangle
class Triangle(Rectangle,Square):
    # defining function
    def tri_area(self, length,breadth):
        area = 0.5*length*breadth
        print("Area of Triangle:", area)
#creating object derived class
obj=Triangle()
obj.rec_area(10,20)
obj.squ_area(12)
obj.tri_area(12,25)
"""
***Output***
Area of Rectangle: 200
Area of Square: 144
Area of Triangle: 150.0
"""

Here Rectangle and Square are Base class and Triangle is derived class so we can call all the functions using object of Triangle class.

Multilevel Inheritance

1.When first class is inherited by second class,second class is inherited by third class and so on called multilevel inheritance.
2.In this type of inheritance each derived class is the base class for the next class.
3.In this type of inheritance atleast three class are compulsory.


 

#creating class
class Rectangle:
    #defining function
    def rec_area(self,height,width):
        area=height*width
        print("Area of Rectangle:",area)
#Inheriting Rectangle into Square
class Square(Rectangle):
    # defining function
    def squ_area(self, side):
        area = side*side
        print("Area of Square:", area)
# Inheriting  Square into Triangle
class Triangle(Square):
    # defining function
    def tri_area(self, length,breadth):
        area = 0.5*length*breadth
        print("Area of Triangle:", area)
#creating object derived class
obj=Triangle()
obj.rec_area(10,20)
obj.squ_area(12)
obj.tri_area(12,25)
"""
***Output***
Area of Rectangle: 200
Area of Square: 144
Area of Triangle: 150.0
"""

Hierarchical Inheritance

1.When a single class is inherited by two or more than two classes simultaneously called hierarchical inheritance.
2.In other word we can say that in this type of inheritance derived class may be two or more than two but Base class should be one.
3.In this type of inheritance atleast three class are compulsory.


 

#creating class
class Rectangle:
    #defining function
    def rec_area(self,height,width):
        area=height*width
        print("Area of Rectangle:",area)
#Inheriting Rectangle into Square
class Square(Rectangle):
    # defining function
    def squ_area(self, side):
        area = side*side
        print("Area of Square:", area)
# Inheriting  Rectangle into Triangle
class Triangle(Rectangle):
    # defining function
    def tri_area(self, length,breadth):
        area = 0.5*length*breadth
        print("Area of Triangle:", area)
#creating object derived class
obj=Triangle()
obj.rec_area(10,20)
obj.tri_area(12,25)
"""
***Output***
Area of Rectangle: 200
Area of Triangle: 150.0
"""
 

In the above example you can see that there are three classes(Rectangle,Square and Triangle) are used in which Rectangle is inherited by Square and Triangle therefore using object of Triangle we can call function only rectangle_area() and tri_area() because there is no relation between Square and Triangle therefore function squ_area() can not be called by object of Triangle.Similarly by using object of class Square we can call only function rec_area() and squ_area().

Hybrid Inheritance

1.The combination of two or more than two inheritance is called Hybrid inheritance.
2.It can be combination of any two or more than two inheritance(single,multiple,multilevel,hierarchical).
3.In this type of inheritance atleast three class are compulsory.


 

#creating class
class Rectangle:
    #defining function
    def rec_area(self,height,width):
        area=height*width
        print("Area of Rectangle:",area)
#Inheriting Rectangle into Square
class Square:
    # defining function
    def squ_area(self, side):
        area = side*side
        print("Area of Square:", area)
# Inheriting  Rectangle into Triangle
class Triangle(Rectangle,Square):
    # defining function
    def tri_area(self, length,breadth):
        area = 0.5*length*breadth
        print("Area of Triangle:", area)
#inheriting Triangle into Circle
class Circle(Triangle):
    # defining function
    def cir_area(self, radius):
        area = 3.14*radius*radius
        print("Area of Circle:", area)
#creating object derived class
obj=Circle()
obj.rec_area(10,20)
obj.squ_area(13)
obj.tri_area(12,25)
obj.cir_area(2.3)
"""
***Output***
Area of Rectangle: 200
Area of Square: 169
Area of Triangle: 150.0
Area of Circle: 16.610599999999998
"""
 

In the above example you can see that there are four classes(Rectangle,Square,Triangle and Circle) in which Rectangle and Square are inherited by Triangle class so in class Rectangle,Square and Triangle there is Multiple inheritance but class Triangle is inherited by Circle so in class Triangle and Circle there is Single inheritance.Therefore the above program is a combination of Multiple and Single inheritance so it is called Hybrid Inheritance.

Advantage of Inheritance

1.Code Reusability: It means function inside base class is shared by all the derived class.
2.Time Saving:Because there is no need to define existing property(same code) of a class in another class.


3.Less Cost:Because existing code is reused, it leads to less development and maintenance costs.
4.It helps to reduce code redundancy.


If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

Thank you!!.