Method Overriding in Python

In this tutorial, let us look at what is method overriding in python along with examples, and its key features.

Method overriding in Python:

Method overriding is a form of Run time polymorphism. The child class provides the specific implementation of the method that is already provided by the parent class. It is used to change the behavior of existing methods, and method overriding requires at least two classes. In method overriding, inheritance is always required because it occurs between methods from the parent class (superclass) and the child class (child class).

parent class: The class that is being inherited is known as the Parent or Superclass.

child class: The class that inherits the parent class’s properties and methods is known as the Child or Subclass.

Features of Method overriding

  • The concept of method overriding is derived from object-oriented programming(OOPS).
  • It allows us to change the implementation of a function in the child class that is defined in the parent class.
  • It is a part of the inheritance concept.
  • Method overriding avoids code duplication while also improving the code by adding some extra properties.

Method overriding Prerequisites 

In Python, there are some prerequisites for method overriding. They are:

  • Method overriding is not possible within a class. As a result, we must derive a child class from a parent class. So, inheritance is required.
  • The method’s name must be the same as in the parent class.
  • The method’s parameters must be the same as those in the parent class.

Approach:

  • Create a parent class say FirstClass
  • Inside the parent class, create a function say display1().
  • Create another function say display2.
  • Create a child class say SecondClass() which inherits properties from the parent class, FirstClass().
  • Create a function with the same name say display1 as in the parent which modifies or overrides the text when it is called.
  • Create an object for the child class say SecondClass() and store it in a variable.
  • Call the display1() function of the SecondClass() which overrides the display1() function of the FirstClass.
  • The Exit of the Program.

Below is the implementation:

# Create a parent class say FirstClass
class FirstClass:
    
    # Create a function say display1
    def display1(self):
        print('display_1 of the FirstClass')
    
    # Create another function say display2
    def display2(self):
        print('display_2 of the FirstClass')
    
# Create a child class say SecondClass which inherits properties from parent class, FirstClass
class SecondClass(FirstClass):
    
    # Create a function with the same name say display1 as in the parent parent
    # which modifies or overrides the text when it is called
    def display1(self):
        print('Overriding display1() class of FirstClass by the SecondClass')
        
    def display3(self):
        print('display_3 of the SecondClass')
        

# Create an object for the child class say SecondClass() and store it in a variable
secondClass_object = SecondClass()
    
# Call the display1() fuction of the SecondClass() which overrides the 
# display1() function of the FirstClass
secondClass_object.display1()

Output:

Overriding display1() class of FirstClass by the SecondClass

Method overriding with Multiple Inheritance

Multiple inheritance occurs when a class is derived from one or more parent(base) classes, inheriting all of its properties.

Overriding in Multiple Inheritance occurs when a child class provides its own implementation of methods/attributes while inheriting them from its parent/parents.

Approach:

  • Create a parent class say Vehicle.
  • Inside the class, create a function say vehicletype.
  • Create another function say brand.
  • Create a class say Twowheeler which inherits properties from the parent class Vehicle.
  • Create another class say Threewheeler which inherits properties from the parent class Vehicle.
  • Create another child class say Fourwheeler which inherits properties from the above both the child classes Twowheeler and Threewheeler.
  • Create an object for the last child class Fourwheeler.
  • Call the vehicletype() function from the Vehicle class and print the result.
  • Call the brand() function from the Vehicle class and print the result.
  • The Exit of the Program.

Below is the implementation:

# Create a parent class say Vehicle
class Vehicle:
    # Inside the class, create a function say vehicletype
    def vehicletype(self):
        print("This gives the type of vehicle")
    # create another function say brand
    def brand(self):
        print("It gives the brand of vehicle")

# Create class say Twowheeler which inherits properties 
# from the parent class Vehicle
class Twowheeler (Vehicle): 
    def vehicletype(self):
        print("Bike is a type of Twowheeler")

# Create another class say Threewheeler which inherits properties 
# from the parent class Vehicle
class Threewheeler (Vehicle): 
    def vehicletype(self):
        print("Auto is a type of Threewheeler")

# Create another child class say Fourwheeler which inherits properties 
# from the above both the child classes Twowheeler and Threewheeler     
class Fourwheeler (Twowheeler, Threewheeler): 
    def vehicletype(self):
        print("Car is a type of Fourwheeler")
        
# Create an object for the last child class Fourwheeler
obj = Fourwheeler() 

# call the vehicletype() function from the Vehicle class and print the result
obj.vehicletype() 
# call the brand() function from the Vehicle class and print the result
obj.brand()

Output:

Car is a type of Fourwheeler
It gives the brand of vehicle

Method overriding with Multi-level Inheritance

Features from both the base and derived classes are inherited into the new derived class in multilevel inheritance. This is comparable to a child and grandfather relationship.

Approach:

  • Create a parent class say Vehicle.
  • Inside the class, create a function say vehicletype.
  • Create another function say brand.
  • Create a child class say Twowheeler which inherits properties from the parent class Vehicle.
  • Create another child class say Threewheeler which inherits properties from the first child class Twowheeler.
  • Create an object for the last child class Threewheeler.
  • Call the vehicletype() function from the Vehicle class and print the result.
  • Call the brand() function from the Vehicle class and print the result.
  • The Exit of the Program.

Below is the implementation:

# Create a parent class say Vehicle
class Vehicle:

    # Inside the class, create a function say vehicletype
    def vehicletype(self):
        print("This gives the type of vehicle")
    # create another function say brand
    def brand(self):
        print("It gives the brand of vehicle")

# Create a child class say Twowheeler which inherits properties 
# from the parent class Vehicle
class Twowheeler(Vehicle): 
    def vehicletype(self):
        print("Bike is a type of Twowheeler")

# Create another child class say Threewheeler which inherits properties 
# from the first child class Twowheeler
class Threewheeler(Twowheeler): 
    def vehicletype(self):
        print("Auto is a type of Threewheeler")

# Create an object for the last child class Threewheeler
obj = Threewheeler() 

# call the vehicletype() function from the Vehicle class and print the result
obj.vehicletype() 
# call the brand() function from the Vehicle class and print the result
obj.brand() 

Output:

Auto is a type of Threewheeler
It gives the brand of vehicle