Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between polymorphism and overloading?

I understand polymorphism and vaguely understand overloading, but would appreciate someone that thoroughly understands there two concepts to explain what the categorical difference is and whether overloading is or is not a form of polymorphism (seems to be disagreement about this).

like image 844
DanielSon Avatar asked Jun 01 '16 01:06

DanielSon


People also ask

What is polymorphisms differentiate between overloading and overriding?

Method overloading is a compile-time polymorphism. Method overriding is a run-time polymorphism. It helps to increase the readability of the program. It is used to grant the specific implementation of the method which is already provided by its parent class or superclass.

Is polymorphism and operator overloading same?

Polymorphism: Polymorphism (or operator overloading) is a manner in which OO systems allow the same operator name or symbol to be used for multiple operations. That is, it allows the operator symbol or name to be bound to more than one implementation of the operator. A simple example of this is the “+” sign.

What is function overloading and polymorphism?

Polymorphism in Function Overloading In computing, polymorphism is a property of object oriented programming in which a function can take different forms based on the number of arguments and their data types. All the functions will have the same name, but they will differ in their arguments.

What is difference between overloading and overriding?

Overriding occurs when the method signature is the same in the superclass and the child class. Overloading occurs when two or more methods in the same class have the same name but different parameters.


1 Answers

Polymorphism, at its core, is about multiple things which all have a certain set of consistent behavior, such that you can replace one with another within a particular algorithm or process. So long as they all provide the expected interface, the process still works.

Overloading doesn't really have such a foundation. It is merely the ability to name two or more functions with the same name, so long as they have different parameter lists. The compiler figure out which function you actually meant based on the types of the arguments you pass.

Now overloading can be used to create polymorphism. Consider the following:

template<typename T>
void func(T t) {call(t);}

This will call call, passing t as a parameter. This will work so long as you provide a type T for which call(t) is legitimate C++ code. You could do this by overloading the function call for whatever types you are interested in using with func:

void call(int);
void call(float);
void call(vector<int>);

In this way, func is a function which is polymorphic (statically) with respect to its parameter. It can perform its operation on any type, so long as that type has the appropriate interface. That interface being the ability to call a function call with a variable of that type.

func(5); //Legal
func(13.4); //Legal
func(vector<int>{4, 3, 2, 1}); //Legal
func(vector<float>{}); //NOT legal

Here, we use function overloading of call to create a form of polymophism through the func function. But this does not mean that overloading is polymorphism.

Overloading is a language tool. Polymorphism is a concept. Polymorphism is about making multiple objects all work the same way. Overloading is just a way to give different functions the same name.

like image 152
Nicol Bolas Avatar answered Oct 12 '22 11:10

Nicol Bolas