Concept of Function Overriding in Python

Mannan Ul Haq
0

Function overriding is a feature in Python that allows a subclass to provide a different implementation of a method that is already defined in its superclass. It lets you redefine the behavior of a method in the subclass, offering specialization or customization according to the subclass's specific needs.


To override a method in Python, the following conditions must be met:


1. Inheritance: The derived class must inherit from the base class.


2. Method Signature: The method in the derived class must have the same name as the method in the base class. However, unlike some other programming languages, Python does not enforce the matching of return types or parameter lists when overriding. It's primarily guided by naming conventions.


Example:


class Parent:
    def print_message(self):
        print("Message from Parent class")

class Child(Parent):
    def print_message(self):
        print("Message from Child class")

child_instance = Child()
child_instance.print_message()  # This will print: Message from Child class


Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !