Method Overloading and Method Overriding in object-Oriented-programming: Deep Dive(C++)
Method overloading and method overriding are two related but distinct concepts in object-oriented programming (OOP) that allow for a more organized and efficient way to structure code.
Method Overloading IN object-Oriented-programming(Oops):
Method overloading is the ability of a class to have multiple methods with the same name but with different parameters. In C++, method overloading is achieved by providing multiple function prototypes with the same name but with different parameters within the same class.
Here is an example of a class Car
which has a method drive
that can be called with different parameters:
In this example, the class Car
has three methods with the same name drive
but with different parameters. The first method takes no parameter, the second one takes an integer parameter and the third one takes a string parameter. The correct version of the method will be called depending on the type and number of the arguments passed to it.
Method Overriding IN object-Oriented-programming(Oops):
Method overriding, on the other hand, is the ability of a subclass to provide a different implementation for a method that is already provided by its superclass. In C++, method overriding is achieved by using the keyword virtual
in the superclass method and override
in the subclass method.
Here is an example of a class hierarchy for a car class where a method is overridden:
I
Comments
Post a Comment