![]() |
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 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. Below is the implementation of function overriding in C++:
Output Dog barks Explanation: In C++, function overriding is achieved using virtual functions. The Animal class declares the makeSound() function as virtual, and the Dog class overrides it. When makeSound() is called on a Dog object, it prints “Dog barks”. 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. Below is the implementation of function overriding in Java:
Output Dog barks Explanation: In this Java example, the Animal class has a method makeSound(). The Dog class inherits from Animal and overrides the makeSound() method with its own implementation. When makeSound() is called on a Dog object, it prints “Dog barks”. 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:
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 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. Below is the implementation of function overriding in C#:
Output Dog barks Explanation: Similar to C++, C# uses the virtual keyword for function overriding. The Dog class overrides the MakeSound() method of the Animal class. When MakeSound() is called on a Dog object, it prints “Dog barks”. 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. Below is the implementation of function overriding in Javascript:
Output Dog barks Explanation: In JavaScript, you can achieve function overriding using class inheritance. The Dog class extends Animal and provides its own implementation of the makeSound() method. When makeSound() is called on a Dog object, it prints “Dog barks”. Advantages of Function Overriding:
Disadvantages of Function Overriding:
ConclusionAs 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. |
Reffered: https://www.geeksforgeeks.org
Programming |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 17 |