What is the difference between polymorphism and overloading?

McLeodx

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).

Nicol Bolas

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

what is the difference between polymorphism and inheritance

From Dev

what is the difference between polymorphism and inheritance

From Dev

operator overloading and polymorphism difference

From Java

What is the difference between dynamic and static polymorphism in Java?

From Dev

Difference between interface and polymorphism

From Dev

What is the difference between compile time polymorphism and static binding?

From Dev

What is the difference between compile time polymorphism and static binding?

From Dev

What's the difference between returning a reference and value when overloading operator=?

From Java

Java static polymorphism (overloading) and inheritance between generics

From Dev

Java static polymorphism (overloading) and inheritance between generics

From Dev

Conceptual difference between parametric polymorphism and subtype polymorphism?

From Dev

Conceptual difference between parametric polymorphism and subtype polymorphism?

From Dev

difference between code polymorphism examples

From Dev

Difference between std::bind and boost::bind with polymorphism

From Dev

Difference between << / >> operator overloading and Input/Output functions

From Dev

Difference between at() and overloading operator [ ] C++

From Dev

difference between constructor overloading and chaining in c#

From Dev

"Polymorphism applies to overriding, not to overloading"?

From Dev

C++ polymorphism and overloading?

From Dev

python polymorphism overloading glitch

From Dev

"Polymorphism applies to overriding, not to overloading"?

From Dev

Java Polymorphism/Overloading surprise

From Dev

What's the difference between "as?", "as!", and "as"?

From Dev

What is the difference in operation between . and ^ and ^(.*)$?

From Dev

What is the difference between .// and //* in XPath?

From Dev

What is the difference between = and => for a variable?

From Dev

What is difference between "?" and "!" in Swift?

From Java

What is the difference between `.` and `#` in the JavaDoc?

From Dev

CoffeeScript, What is the difference between => and ->

Related Related

  1. 1

    what is the difference between polymorphism and inheritance

  2. 2

    what is the difference between polymorphism and inheritance

  3. 3

    operator overloading and polymorphism difference

  4. 4

    What is the difference between dynamic and static polymorphism in Java?

  5. 5

    Difference between interface and polymorphism

  6. 6

    What is the difference between compile time polymorphism and static binding?

  7. 7

    What is the difference between compile time polymorphism and static binding?

  8. 8

    What's the difference between returning a reference and value when overloading operator=?

  9. 9

    Java static polymorphism (overloading) and inheritance between generics

  10. 10

    Java static polymorphism (overloading) and inheritance between generics

  11. 11

    Conceptual difference between parametric polymorphism and subtype polymorphism?

  12. 12

    Conceptual difference between parametric polymorphism and subtype polymorphism?

  13. 13

    difference between code polymorphism examples

  14. 14

    Difference between std::bind and boost::bind with polymorphism

  15. 15

    Difference between << / >> operator overloading and Input/Output functions

  16. 16

    Difference between at() and overloading operator [ ] C++

  17. 17

    difference between constructor overloading and chaining in c#

  18. 18

    "Polymorphism applies to overriding, not to overloading"?

  19. 19

    C++ polymorphism and overloading?

  20. 20

    python polymorphism overloading glitch

  21. 21

    "Polymorphism applies to overriding, not to overloading"?

  22. 22

    Java Polymorphism/Overloading surprise

  23. 23

    What's the difference between "as?", "as!", and "as"?

  24. 24

    What is the difference in operation between . and ^ and ^(.*)$?

  25. 25

    What is the difference between .// and //* in XPath?

  26. 26

    What is the difference between = and => for a variable?

  27. 27

    What is difference between "?" and "!" in Swift?

  28. 28

    What is the difference between `.` and `#` in the JavaDoc?

  29. 29

    CoffeeScript, What is the difference between => and ->

HotTag

Archive