Python Inheritance



The process of getting the property of one class into another class is called Inheritance.

In other words, we can say that deriving a new class from an old class is called inheritance in which the new class is called derived or child or subclass, and the old class is called Base or Parent or Superclass.

When a class inherits a class's property, it can access all the data member and member functions of that class except the private element.

Python InheritanceIn this programming mainly two types of classes are used.

  • Parent class (superclass)
  • Child class (subclass)

Parent class (superclass)

The class which is inherited by another class is called Parent or Super or Base class.

Child class (subclass)

The class which inherits the property of another class is called Child or Sub or Derived class.

How to inherit one class into another? 


Example

class Rectangle:
    # Define function
    def rect_area(self, height, width):
        area = height * width
        print("Area of Rectangle:", area)

# Inherite Rectangle into Square
class Square(Rectangle):
    # Define function
    def squa_area(self, side):
        area = side * side
        print("Area of Square:", area)


# Create a object
obj = Square()

# call base class function
obj.rect_area(5, 15)

# call sub class function
obj.squa_area(11)

Output

Area of Rectangle: 75
Area of Square: 121

Here, class Rectangle is a base class and Square is a subclass because Rectangle is inherited into Square hence we can call all the functions using the object of Square.

Multiple Inheritance

When two or more than two classes are inherited by a single class together called multiple inheritances.

In other words, we can say that in this type of inheritance Base class maybe two or more than two but the derived class should be one.


Example

# Create class
class Rectangle:
    # Define function
    def rect_area(self, height, width):
        area = height * width
        print("Area of Rectangle:", area)

# Create class
class Square:
    # Define function
    def squa_area(self, side):
        area = side * side
        print("Area of Square:", area)

# Inherite Rectangle and Square into Triangle
class Triangle(Rectangle, Square):
    # Define function
    def tria_area(self, length, breadth):
        area = 0.5 * length * breadth
        print("Area of Triangle:", area)


# creating object derived class
obj = Triangle()
obj.rect_area(5, 15)
obj.squa_area(11)
obj.tria_area(4, 3)

Output

Area of Rectangle: 75
Area of Square: 121
Area of Triangle: 6.0

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

Multilevel Inheritance

When first-class is inherited by the second class, second class is inherited by the third class, and so on called multilevel inheritance.

Multilevel inheritance, each derived class is the base class for the next class.


Example

# Create class
class Rectangle:
    # Define function
    def rect_area(self, height, width):
        area = height * width
        print("Area of Rectangle:", area)

# Create class
class Square(Rectangle):
    # Define function
    def squa_area(self, side):
        area = side * side
        print("Area of Square:", area)

# Inherite Rectangle and Square into Triangle
class Triangle(Square):
    # Define function
    def tria_area(self, length, breadth):
        area = 0.5 * length * breadth
        print("Area of Triangle:", area)


# creating object derived class
obj = Triangle()
obj.rect_area(5, 15)
obj.squa_area(11)
obj.tria_area(4, 3)

Output

Area of Rectangle: 75
Area of Square: 121
Area of Triangle: 6.0

Hierarchical Inheritance

When a single class is inherited by two or more than two classes simultaneously called hierarchical inheritance.

In other words, we can say that in this type of inheritance derived class maybe two or more than two but the Base class should be one


Example

# Create class
class Rectangle:
    # Define function
    def rect_area(self, height, width):
        area = height * width
        print("Area of Rectangle:", area)

# Create class
class Square(Rectangle):
    # Define function
    def squa_area(self, side):
        area = side * side
        print("Area of Square:", area)

# Inherite Rectangle and Square into Triangle
class Triangle(Rectangle):
    # Define function
    def tria_area(self, length, breadth):
        area = 0.5 * length * breadth
        print("Area of Triangle:", area)


# creating object derived class
obj = Triangle()
obj.rect_area(5, 15)
obj.tria_area(4, 3)

Output

Area of Rectangle: 75
Area of Triangle: 6.0

In the above example, We 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 the function only rect_area() and tria_area() because there is no relation between Square and Triangle, therefore, function squa_area() can not be called by the object of Triangle. Similarly, by using the object of class Square we can call only the function rect_area() and squa_area().

Hybrid Inheritance

The combination of two or more than two inheritance is called Hybrid inheritance.

It can be a combination of any two or more than two inheritance(single, multiple, multilevel, hierarchical).


Example

# Create class
class Rectangle:
    # Define function
    def rect_area(self, height, width):
        area = height * width
        print("Area of Rectangle:", area)

# Create class
class Square:
    # Define function
    def squa_area(self, side):
        area = side * side
        print("Area of Square:", area)

# Inherite Rectangle and Square into Triangle
class Triangle(Rectangle, Square):
    # Define function
    def tria_area(self, length, breadth):
        area = 0.5 * length * breadth
        print("Area of Triangle:", area)

# Inherite Triangle into Circle
class Circle(Triangle):
    # Define function
    def cir_area(self, radius):
        area = 3.14*radius*radius
        print("Area of Circle:", area)


# creating object derived class
obj = Circle()
obj.rect_area(5, 15)
obj.squa_area(11)
obj.tria_area(4, 3)
obj.cir_area(5)

Output

Area of Rectangle: 75
Area of Square: 121
Area of Triangle: 6.0
Area of Circle: 78.5

Advantage of Inheritance

Code Reusability: It means the function inside the base class is shared by all the derived classes.

Time-Saving: Because there is no need to define the existing property(same code) of a class in another class.

Less Cost: Because existing code is reused, it leads to less development and maintenance costs.

It helps to reduce code redundancy.