Method of Overriding

Method Overriding

In OOP, we use Method Overriding or also known as Function Overriding, to override a function that exists in the parent class. In Inheritance, we can use Function Overriding to create a unique child class that reuses existing code with modifications to selected portions of our code. This is a powerful concept that we can use in Python to create powerful and useful code.

Consider an example:

class Animal:

    def __init__(self, legs, sound):
        self.legs = legs
        self.sound = sound

    def make_sound(self):
        print("Makes a sound!")

    def count_legs(self):
        print("This animal has", self.legs, "legs")


class Lion(Animal):

    def make_sound(self):
        print("Lion is a", self.legs, "legged, and makes the sound", self.sound)


# Creating an object of class Lion
animal_1 = Lion(4, "Roar!")

print("Function from Parent Class")
animal_1.count_legs()


print("")

# Function from child class overrides the function in the parent class
print("Function from Child Class")
animal_1.make_sound()

 

This gives us the output:

We created a parent class called “Animal” and then we created a child class called “Lion”. We initialized the Animal class with a function to print a sound, and then in the child class, we initialized another function with the same name that print its code. Now because of Method Overriding or Function Overriding when we create an object of the child class it will print the statement from the function in the child class instead of the print statement from the function in the parent class.

Let us look at another scenario where Function Overriding can come in handy. Let’s consider a scenario where we have to write a program for a shop that sells Furniture. First, we want to show a welcome message when the user enters the store and then a message for each department.

class Furniture:

    def __init__(self):
        self.name = "Sunstore Furniture Company"

    def show_advertisement(self):
        print("Welcome to", self.name, "we sell the latest furniture!")


class Tables(Furniture):

    def show_advertisement(self):
        print("Welcome to", self.name, "in this department we have the newest Tables")


class Chairs(Furniture):

    def show_advertisement(self):
        print("Welcome to", self.name, "in this department we have the newest Chairs")


print("Customer 1 enters the main shop, and the boss says:")
customer_1 = Furniture()
customer_1.show_advertisement()

print("")
print("Customer 2 enters the Tables department, and the salesman says:")
customer_2 = Tables()
customer_2.show_advertisement()

print("")
print("Customer 3 enters the Chairs department, and the salesman says:")
customer_3 = Chairs()
customer_3.show_advertisement()

 

We get this as the result:

We created 3 objects, one object was of the parent class and two of the remaining objects were of the child classes. The first object prints the general message and the remaining objects print statements which are messages unique for each department.

All 3 objects have the same function to display a welcome message and each function has the same name. Here the base class has defined for us the name of the company and thanks to OOP and Function Overriding we only have to re-define the function contents for each department. Thus, we have simplistically reused code.

What have we learned?

  • What is another name for Method Overriding?
  • How does Method Overriding help in programming?
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments