Function overriding in Python

Function overriding comes to Python quite easily and it is also supported due to Python being a dynamic language. Inheritance makes it possible for subclasses to override methods from a superclass by mere demonstration. It is also possible to define classes in Python that feature an inheritance mechanism where subclasses can inherit and/or override methods of a parent class.

Below is the implementation of function overriding in Python:

Python
class Animal:
    def make_sound(self):
        print("Animal makes a sound")

class Dog(Animal):
    def make_sound(self):
        print("Dog barks")

animal = Dog()
animal.make_sound() # Output: Dog barks

Output
Dog barks

Explanation: In Python, function overriding is straightforward. The Dog class inherits from Animal and provides its own implementation of the make_sound() method. When make_sound() is called on a Dog object, it prints “Dog barks”.

Function overriding in programming

Function Overriding is a fundamental principle in object-oriented programming wherein the subclass implements a specific method that has been declared in the superclass. This concept would enable the method calls to be polymorphic where the same method call may behave differently depending on the object which initiated the method call. In this article, we will discuss the basics of Function Overriding along with its implementation in different languages.

Table of Content

  • What is Function overriding?
  • Function overriding in C++
  • Function overriding in Java
  • Function overriding in Python
  • Function overriding in C#
  • Function overriding in Javascript
  • Advantages of Function Overriding
  • Disadvantages of Function Overriding

Similar Reads

What is Function overriding?

Function overriding is when a function in the base class is redefined in the derived class to provide a different implementation of the function for the derived class. The function in the derived class has the same function signature as the function in the base class (same name, same return type, same arguments). Function overriding provides a way to implement polymorphism....

Function overriding in C++:

C++ provides function overriding with the use of virtual functions in inheritance. In case the function in the base class is declared as virtual and is overridden in the derived class, the derived class’s implementation will be called if the method is called through the reference or pointer of the base class....

Function overriding in Java:

Method overriding is a core of Java wherein a subclass has the ability to override or provide its own version of a method that is in the superclass. Sometimes in Java the @Override annotation can be used which ensures that the particular method is intended to override the specific method of the superclass....

Function overriding in Python:

Function overriding comes to Python quite easily and it is also supported due to Python being a dynamic language. Inheritance makes it possible for subclasses to override methods from a superclass by mere demonstration. It is also possible to define classes in Python that feature an inheritance mechanism where subclasses can inherit and/or override methods of a parent class....

Function overriding in C#:

Overriding can be achieved in C# by the using the override keyword. This works well in cases where a subclass may want to override methods from its base classes in order to provide custom implementations. C# also has a concept of using virtual keyword in base class to indicate that a method in the base class can be overridden by the sub class....

Function overriding in Javascript:

Even though JavaScript is not a classical language that supports classical inheritance and function overriding others do JavaScript does support prototype-based inheritance. Function overriding can be implemented in various ways such as changing the prototypes of objects or by using ES6 classes and the keyword extends....

Advantages of Function Overriding:

Code Reusability: Method overriding enables subordinates to extend methods developed in superordinates. This helps in preventing the same piece of code from being written in different classes which deal with similar functionalities.Polymorphism: Overriding helps in achieving polymorphism as it allows different methods of the parent class to be invoked depending on the instance of the object that triggered the method. Modularity: Function overriding also encourages the concept of modularity in code design because it allows subclasses to override specific methods. Specialization: Method overriding can be defined as a process that allows the subclasses in object oriented programming to alter the behavior of the methods inherited from the super classes....

Disadvantages of Function Overriding:

Complexity: Using deep hierarchy inheritance in combination with function overriding of multiple levels may result in complexity of the source code. Maintenance Challenges: Function overriding will make code maintenance more complicated and a change to super class will affect all subclasses that override some methods in super class. Lack of Clarity: There are instances where overriding functions may hide the method and what they do, particularly if the methods implemented in the derived classes are entirely unique....

Conclusion

As such, function overriding is a very important aspect in the area of object-oriented programming because it provides a mechanism through which functionality could be provided by the subclasses of an object. This means the creation of code that can be reused, modularized, and demonstrated polymorphic behavior which helps in making the software extensible....