Python Inheritance


Inheritance

Inheritance is a way to create a new class that inherits the methods and properties of the existing class.

The existing class is said to be the base class or the parent class, and the new class is said to be the derived class or the child class.

Syntax:

In [1]:
class parent:
    pass
class child(parent):
    pass
Inheritance

Creating a Parent Class

The parent class is created in the same way as we normally create a class in python.

In [1]:
class car:
    def __init__(self, brand, seating_capacity):
        self.brand = brand
        self.seating_capacity = seating_capacity
    
    def car_details(self):
        print("This is a car.")

Creating a Child Class

The parent class is sent as the parameter in parentheses while creating the child class.

Example:

In [1]:
class car:
    brand = ""
    
    def car_details(self):
        print("This is a car.")
        
        
class electric_car(car):
    def electric_car_details(self):
        print("This is a electric car. Made By", self.brand)
In [2]:
e_car = electric_car()
e_car.brand = "Ford"

e_car.car_details()
e_car.electric_car_details()
This is a car.
This is a electric car. Made By Ford

Calling Constructor of Parent Class

From the child class, we can call the constructor of the parent class, means we can call the __init__() method of the parent class from the __init__ method of the child class. Like this, we can initialize the variables of the parent class from the child class.

Syntax:

<parentclassname>.__init__(self, <parameters>)

Example:

In [1]:
class car:
    def __init__(self, brand, seating_capacity):
        self.brand = brand
        self.seating_capacity = seating_capacity
    
    def car_details(self):
        print("This is a car.")
In [2]:
class electric_car(car):
    def __init__(self, model, cartype, brand, seating_capacity):
        car.__init__(self, brand, seating_capacity)
        self.model = model
        self.cartype = cartype
    
    def elec_car_details(self):
        print("This is an electric car.")
In [3]:
model_3 = electric_car("Model 3", "Sedan", "Tesla", 5)
In [4]:
model_3.model
Out[4]:
'Model 3'
In [5]:
model_3.car_details()
This is a car.
In [6]:
model_3.brand
Out[6]:
'Tesla'

Method Overriding

A subclass has all the methods of the parent class, but in some cases a subclass may have different requirements than the parent class. In this case, a new method with the same name and same parameters can be defined in the child class with additional functionalities. This is called method overriding. Means method of the child class with the same name and parameters will override the method of the parent class.

Method Overriding
In [1]:
class shape:
    def draw(self):
        print("Draw a shape.")

class circle(shape):
    def draw(self):
        print("Draw a circle.")
        
In [2]:
shape1 = circle()
shape1.draw()
Draw a circle.

The super() Method

In the child class, if we want to use the method of the super class, then the super() method is used.

In [1]:
class shape:
    def draw(self):
        print("Draw a shape.")

class circle(shape):
    def draw(self):
        super().draw()
        print("Draw a circle.")
        
shape1 = circle()
shape1.draw()
Draw a shape.
Draw a circle.

Advantages of Inheritance

1. Code Reusability : Once the baser class code is written and tested properly, we can reuse in the derived classes.

2. Easy to modify code : To make changes in the derived classes, we can simply make changes in the parent class, which will automatically be reflected in the derived class.

3. Consistent behavior : When a parent class is inherited by multiple classes, it ensures the same behavior in all the child classes.

4. Saves development time : As we are re-using the already written code, so it saves time of the developers as they need not rewrite the same code.

5. Abstraction : As we are not concerted about the code implementation of the base class, so it provides the abstraction for the base class code.

Disadvantages of Inheritance

1. Increases Code Complexity : Improper or the overuse of the inheritance can increase the complexity of the code.

2. Need to understand the hierarchy : To make inheritance work properly and give the desired result, user needs to understand the hierarchy properly, otherwise, chances that that user can mess the results.

Types of inheritance:

  1. Single Inheritance : A child class is derived from a single parent class.
  2. Multiple Inheritance : A child class is derived from more than one base class.
  3. Multilevel Inheritance : A class is derived from a child class.
  4. Hierarchical Inheritance : When more than one child class is inherited from a single base class.
  5. Hybrid Inheritance : Combination of multiple types of inheritance.

References:

  1. Inheritance