Python Program to Implement Overriding

Introduction

Method overriding is a feature of object-oriented programming that allows a subclass to provide a specific implementation of a method that is already defined in its superclass. The method in the subclass should have the same name, return type, and parameters as the method in the superclass. When a method is overridden, the version of the method that gets called is determined by the type of the object that invokes it, even if the object is being referenced by a variable of the superclass type.

This tutorial will guide you through creating a Python program that demonstrates method overriding by defining a parent class and a child class, where the child class overrides a method from the parent class.

Example:

  • Parent Class: Animal
  • Child Class: Dog
  • Methods: sound() (overridden in Dog)
  • Program Output:
    The animal makes a sound
    The dog barks
    

Problem Statement

Create a Python program that:

  • Defines a parent class named Animal with a method sound that prints a generic message.
  • Defines a child class named Dog that inherits from Animal and overrides the sound method to print a specific message.
  • Creates objects of both Animal and Dog and calls the sound method to demonstrate method overriding.

Solution Steps

  1. Define the Parent Class: Use the class keyword to define a class named Animal.
  2. Add a Method to the Parent Class: Define a method named sound that prints a generic message.
  3. Define the Child Class: Use the class keyword to define a class named Dog that inherits from Animal.
  4. Override the Method in the Child Class: Define a method named sound in the Dog class that prints a specific message.
  5. Create Objects: Instantiate objects of both the Animal and Dog classes.
  6. Call the Methods: Call the sound method on both objects to demonstrate method overriding.

Python Program

# Python Program to Implement Method Overriding
# Author: https://www.rameshfadatare.com/

# Step 1: Define the Parent Class
class Animal:
    # Step 2: Add a Method to the Parent Class
    def sound(self):
        print("The animal makes a sound")

# Step 3: Define the Child Class
class Dog(Animal):
    # Step 4: Override the Method in the Child Class
    def sound(self):
        print("The dog barks")

# Step 5: Create Objects
generic_animal = Animal()
pet_dog = Dog()

# Step 6: Call the Methods to Demonstrate Method Overriding
generic_animal.sound()  # Calls the method from Animal class
pet_dog.sound()         # Calls the overridden method from Dog class

Explanation

Step 1: Define the Parent Class

  • The Animal class is defined using the class keyword. This class serves as the parent (or base) class.

Step 2: Add a Method to the Parent Class

  • The sound method is defined in the Animal class. It prints a generic message indicating that an animal makes a sound.

Step 3: Define the Child Class

  • The Dog class is defined, inheriting from the Animal class. This is indicated by the syntax class Dog(Animal):.

Step 4: Override the Method in the Child Class

  • The sound method is redefined in the Dog class. This method overrides the sound method in the Animal class and prints a specific message indicating that a dog barks.

Step 5: Create Objects

  • Two objects are created: generic_animal is an instance of the Animal class, and pet_dog is an instance of the Dog class.

Step 6: Call the Methods

  • The sound method is called on both generic_animal and pet_dog. The method called depends on the type of the object:
    • generic_animal.sound() calls the method from the Animal class.
    • pet_dog.sound() calls the overridden method from the Dog class.

Output Example

Example Output:

The animal makes a sound
The dog barks

Additional Examples

Example 1: Method Overriding with More Complex Methods

# Define a parent class Vehicle
class Vehicle:
    def description(self):
        print("This is a vehicle")

# Define a child class Car that overrides the description method
class Car(Vehicle):
    def description(self):
        print("This is a car")

# Create objects of both classes and call the description method
generic_vehicle = Vehicle()
family_car = Car()

generic_vehicle.description()  # Calls the method from Vehicle class
family_car.description()        # Calls the overridden method from Car class

Output:

This is a vehicle
This is a car

Example 2: Method Overriding with Superclass Method Call

# Define a parent class Shape
class Shape:
    def draw(self):
        print("Drawing a shape")

# Define a child class Circle that overrides the draw method
class Circle(Shape):
    def draw(self):
        super().draw()  # Call the method from the parent class
        print("Drawing a circle")

# Create an object of the Circle class and call the draw method
my_circle = Circle()
my_circle.draw()

Output:

Drawing a shape
Drawing a circle

Example 3: Method Overriding with Additional Functionality

# Define a parent class Employee
class Employee:
    def work(self):
        print("Employee is working")

# Define a child class Manager that overrides the work method
class Manager(Employee):
    def work(self):
        print("Manager is planning")
        super().work()  # Call the method from the Employee class

# Create an object of the Manager class and call the work method
manager = Manager()
manager.work()

Output:

Manager is planning
Employee is working

Conclusion

This Python program demonstrates how to implement method overriding, a key concept in object-oriented programming. Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass, enabling polymorphism. Understanding method overriding is crucial for designing flexible and reusable class hierarchies in Python.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top