Function Overriding in Python


Function Overriding in Python occurs when a subclass (child class) provides a specific implementation of a method that is already defined in its superclass (parent class). The child class "overrides" the method from the parent class by redefining it with its own implementation.

Key Concepts of Function Overriding:

  1. Inheritance: The subclass inherits from the parent class.

  2. Method Overriding: The subclass defines a method with the same name and signature as the parent class method.

  3. Super Function: The super() function can be used to call the method from the parent class if needed.

Syntax of Function Overriding:

  • class ParentClass:

  •     def method(self):

  •         print("This is the method in Parent Class")


  • class ChildClass(ParentClass):

  •     def method(self):

  •         print("This is the overridden method in Child Class")


In this case, the ChildClass overrides the method that is defined in ParentClass.

How Function Overriding Works:

  • The child class provides its own version of the method, which replaces the method of the parent class in the child class object.

  • When you call the method on an instance of the child class, the child class's method will be executed, not the parent class's method.

Example of Function Overriding:

  • # Parent class

  • class Animal:

  •     def sound(self):

  •         print("Animals make sounds")


  • # Child class that overrides the sound method

  • class Dog(Animal):

  •     def sound(self):

  •         print("Dog barks")


  • class Cat(Animal):

  •     def sound(self):

  •         print("Cat meows")


  • # Creating instances of Dog and Cat

  • dog = Dog()

  • cat = Cat()


  • # Calling overridden methods

  • dog.sound()  # Output: Dog barks

  • cat.sound()  # Output: Cat meows


Explanation:

  • The sound() method in the Animal class is overridden in both the Dog and Cat classes.

  • When calling dog.sound(), the Dog class's sound() method is called.

  • Similarly, when calling cat.sound(), the Cat class's sound() method is called.

Using super() with Function Overriding:

In cases where you want to call the parent class's method in addition to the overridden method in the child class, you can use the super() function.

  • class Animal:

  •     def sound(self):

  •         print("Animals make sounds")


  • class Dog(Animal):

  •     def sound(self):

  •         super().sound()  # Call the parent class's sound method

  •         print("Dog barks")


  • # Creating an instance of Dog

  • dog = Dog()


  • # Calling the overridden method with super()

  • dog.sound()


Output:

  • Animals make sounds

  • Dog barks


Explanation:

  • The super().sound() inside the Dog class calls the sound() method from the Animal class (parent class) before printing "Dog barks".

  • This way, both the parent and child class methods are executed.

When to Use Function Overriding:

  • Customization: You override a parent class method in the child class when you need to provide specific behavior that differs from the parent class's implementation.

  • Specialization: When a parent class provides a generic method, the child class can override it to add specialized functionality.

Example with Arguments in Function Overriding:

  • class Vehicle:

  •     def start_engine(self, fuel_type):

  •         print(f"Starting the engine with {fuel_type} fuel.")


  • class ElectricCar(Vehicle):

  •     def start_engine(self, fuel_type):

  •         if fuel_type != "electricity":

  •             print("Electric car can only start with electricity.")

  •         else:

  •             print("Starting electric car with electricity.")

  •     

  • # Creating instances

  • car = ElectricCar()


  • # Calling overridden method with different arguments

  • car.start_engine("gas")  # Output: Electric car can only start with electricity.

  • car.start_engine("electricity")  # Output: Starting electric car with electricity.


Explanation:

  • The ElectricCar class overrides the start_engine method to add a check for the type of fuel.

  • When the fuel type is "gas", the method prints an error message, but when it's "electricity", the method works as expected.

Advantages of Function Overriding:

  1. Customization: You can provide specific behavior for a method in the child class while still maintaining the general structure in the parent class.

  2. Polymorphism: Overriding allows you to use the same method name to perform different tasks depending on the class of the object. This is a key concept in polymorphism.

Challenges of Function Overriding:

  1. Unintended Overriding: You might unintentionally override a method and lose functionality from the parent class. Always ensure that the child class's version is necessary.

  2. Complexity: If the class hierarchy is deep, it might become difficult to track which method is being called from which class, especially when using super().

Conclusion:

  • Function overriding allows a subclass to change or extend the behavior of a method defined in its superclass.

  • You can use the super() function to call the parent class's method in the child class.

  • Overriding provides customized functionality and is a key aspect of polymorphism in object-oriented programming.

Let me know if you need more examples or clarification! 😊


Post a Comment

0 Comments