28

Is Method Overloading considered part of polymorphism?

2
  • 2
    This question was discussed on Meta, and some people have decided to close as opinion-based, as if “polymorphism” had no clear definition (also claimed by the accepted answer). This is wrong, so I've voted to reopen. The current Polymorphism entry in Wikipedia is fine and matches the academic definitions in Pierce “Types and Programming Languages”, Cardelli & Wegner “On understanding types, data abstraction, and polymorphism”, and originally Strachey “Fundamental Concepts in Programming Languages”.
    – amon
    Dec 3, 2018 at 10:54
  • In Java, as per Kathy Sierra's book, it is stated that "Polymorphism applies to only overriding, not to overloading" in the Drills section of the 2nd chapter Apr 14, 2019 at 13:10

8 Answers 8

43

There are different types of polymorphism:

  • overloading polymorphism (also called Ad-hoc polymorphism)
  • overriding polymorphism

So yes it is part of polymorphism.

7
  • 3
    You need to mention, that "This section's factual accuracy is disputed" so it is not yet stated 100% that overloading is polymorphism as I understand. Mar 8, 2010 at 9:30
  • 2
    @FractalizeR maybe your understanding is limited to runtime polymorphism in object oriented languages, not the term in general Jul 4, 2010 at 9:19
  • 1
    I am just citing WikiPedia here ;) Jul 4, 2010 at 9:54
  • 3
    @FractalizeR: Strachey defines what Darin says. THAT part of the section is not factually disputed. (At least, not by anyone who's actually read the source papers.) Feb 14, 2011 at 9:42
  • But I got in "Head First Java" ( 2nd Edition ) that, "There's no polymorphism involved with overloaded methods !" ............ :/
    – Mukit09
    Aug 1, 2016 at 10:48
21

"Polymorphism" is just a word and doesn't have a globally agreed-upon, precise definition. You will not be enlightened by either a "yes" or a "no" answer to your question because the difference will be in the chosen definition of "polymorphism" and not in the essence of method overloading as a feature of any particular language. You can see the evidence of that in most of the other answers here, each introducing its own definition and then evaluating the language feature against it.

2
  • This was asked in interview for me. 'Explain polymorphism (in Java)'. Now, should I mention overloading or not? Feb 21, 2022 at 16:31
  • 1
    In Java, "polymorphism" refers to "runtime polymorphysm", which means dynamic method dispatch. Overloading is not considered an example of polymorphism; in fact, Java uses the concept of method signature that includes its name as well as its parameter types. When you look at it that way, two methods sharing the name is "just a coincidence". Feb 22, 2022 at 12:11
13

Strictly speaking polymorphism, from wikipedia:

is the ability of one type, A, to appear as and be used like another type, B.

So, method overloading as such is not considered part of this definition polymorphism, as the overloads are defined as part of one type.

If you are talking about inclusion polymorphism (normally thought of as overriding), that is a different matter and then, yes it is considered to be part of polymorphism.

inclusion polymorphism is a concept in type theory wherein a name may denote instances of many different classes as long as they are related by some common super class.

12

There are 2 types of polymorphism.

  1. static
  2. dynamic.

Overloading is of type static polymorphism.. overriding comes under dynamic (or run-time) polymorphism..

ref. http://en.wikipedia.org/wiki/Polymorphism_(computer_science) which describes it more.

9

No, overloading is not. Maybe you refer to method overriding which is indeed part of polymorphism.

To further clarify, From the wikipedia:

Polymorphism is not the same as method overloading or method overriding.1 Polymorphism is only concerned with the application of specific implementations to an interface or a more generic base class.

So I'd say method overriding AND method overloading and convenient features of some language regarding polymorphism but notthe main concern of polymorphism (in object oriented programming) which only regards to the capability of an object to act as if it was another object in its hierarchy chain.

8
  • 4
    See itu.dk/courses/BPRD/E2009/fundamental-1967.pdf for why this is wrong. Feb 14, 2011 at 9:46
  • 5
    See lucacardelli.name/Papers/OnUnderstanding.A4.pdf (section 1.3 in particular) for a possibly easier read of why this is wrong.
    – overthink
    Jun 1, 2012 at 19:52
  • 1
    Wikipedia is not reliable ... you should always go to sources.
    – Jim Balter
    Mar 30, 2013 at 3:06
  • 2
    @GrijeshChauhan Inapplicable article: "Should you use or cite Wikipedia ->>> as a source for an academic paper <<<- ? " ... There's nothing wrong with using WP but, like I said, you should go to sources ... the best thing about WP is its citations.
    – Jim Balter
    Oct 29, 2013 at 20:00
  • 1
    @GrijeshChauhan That's what I said. But you cited an inapplicable article, since SO answers aren't academic papers.
    – Jim Balter
    Nov 1, 2013 at 0:45
5

Method overriding or overloading is not polymorphism. The right way to put it is that Polymorphism can be implemented using method overriding or overloading and using other ways as well.

In order to implement Polymorphism using method overriding, you can override the behaviour of a method in a sub-class.

In order to implement Polymorphism using method overloading, you need to write many methods with the same name and the same number of parameters but with different data types and implement different behavious in these methods. Now that is also polymorphism.

Other ways to implement polymorphism is operator overloading and implementing interfaces.

2

Wikipedia pedantics aside, one way to think about polymorphism is: the ability for a single line of code / single method call to do different things at runtime depending on the type of the object instance used to make the call.

Method overloading does not change behaviors at runtime. Overloading gives you more choices for argument lists on the same method name when you're writing and compiling the code, but when it's compiled the choice is fixed in code forever.

Not to be confused with method overriding, which is part of polymorphism.

1
  • 1
    That's one narrow definition of polymorphism; there are definitions that state that it's more than just about runtime behaviour. (Either way, consider also a language like Groovy, where overloads are resolved at runtime.) Dec 26, 2013 at 12:15
-6

It's a necessary evil that is and should only be used as a complement. In the end overloads should only convert and eventually forward to the main method. OverloDing is necessary because most vms for staticalky dispatched environments don't know how to convert one type to another so the parameter fits the target and this is where one uses overloads to help out.

 StringBuilder
     Append(String) // main
     Append(Boolean) // converts and calls append(String)
1
  • 2
    Please answer the question, rather than commenting on the general topic. Also, please check your spelling. Nov 8, 2014 at 23:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.