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:
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