28

Is Method Overloading considered part of polymorphism?

George Stocker
  • 57,289
  • 29
  • 176
  • 237
iTayb
  • 12,373
  • 24
  • 81
  • 135
  • 2
    This question was [discussed on Meta](https://meta.stackoverflow.com/q/377326), 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](https://en.wikipedia.org/?oldid=864978178) 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 03 '18 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 – Vyshnav Ramesh Thrissur Apr 14 '19 at 13:10

8 Answers8

44

There are different types of polymorphism:

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

So yes it is part of polymorphism.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 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. – Vladislav Rastrusny Mar 08 '10 at 09:30
  • 2
    @FractalizeR maybe your understanding is limited to runtime polymorphism in object oriented languages, not the term in general – Pete Kirkham Jul 04 '10 at 09:19
  • 1
    I am just citing WikiPedia here ;) – Vladislav Rastrusny Jul 04 '10 at 09: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.) – Frank Shearar Feb 14 '11 at 09:42
  • But I got in "Head First Java" ( 2nd Edition ) that, "There's no polymorphism involved with overloaded methods !" ............ :/ – Mukit09 Aug 01 '16 at 10:48
  • Main point of polymorphism is that a bunch of objects conforming to the same abstraction or interface (say "Shape") exhibit different behaviour when the methods of the interface are invoked. Hence the term "polymorphism" that is "many shapes". That is, if I call the same method, say "draw()" on any of the objects from that bunch, they may produce different results. Overriding allows to achieve polymorphism. Overloading creates new methods (parameters are part of the method signature) hence they are no longer part of original interface. Hence overloading has nothing to do with polymorphism. – avepr Dec 28 '20 at 06:50
  • @avepr That depends on definitions. You are citing quite strict definition of methods and polymorphism, but that not the only possible. Overriding pn JVM technically also creates new methods in fact and they even may have different signatures (up to variance). So yes, overloading is a type of polymorphism if you pick definition that fits. – Rorick May 12 '22 at 14:59
20

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

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • This was asked in interview for me. 'Explain polymorphism (in Java)'. Now, should I mention overloading or not? – Sourav Kannantha B Feb 21 '22 at 16:31
  • 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". – Marko Topolnik Feb 22 '22 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.

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
Oded
  • 489,969
  • 99
  • 883
  • 1,009
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.

raj
  • 3,769
  • 4
  • 25
  • 43
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.

Jorge Córdoba
  • 51,063
  • 11
  • 80
  • 130
  • 4
    See http://www.itu.dk/courses/BPRD/E2009/fundamental-1967.pdf for why this is wrong. – Frank Shearar Feb 14 '11 at 09:46
  • 5
    See http://lucacardelli.name/Papers/OnUnderstanding.A4.pdf (section 1.3 in particular) for a possibly easier read of why this is wrong. – overthink Jun 01 '12 at 19:52
  • 1
    Wikipedia is not reliable ... you should always go to sources. – Jim Balter Mar 30 '13 at 03:06
  • Yes @JimBalter Wikipedia is not considered reliable source,[Should I use or cite Wikipedia? Probably not](http://library.williams.edu/citing/wikipedia.php) – Grijesh Chauhan Oct 29 '13 at 14:03
  • 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 '13 at 20:00
  • @JimBalter What I mean is that WP is not fully reliable. – Grijesh Chauhan Oct 30 '13 at 03:56
  • 1
    @GrijeshChauhan That's what *I* said. But *you* cited an inapplicable article, since SO answers aren't academic papers. – Jim Balter Nov 01 '13 at 00:45
  • That's one narrow definition of polymorphism; there are definitions that state that it's more than just about runtime behaviour. – Oliver Charlesworth Dec 26 '13 at 12:13
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.

3

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.

dthorpe
  • 35,318
  • 5
  • 75
  • 119
  • 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.) – Oliver Charlesworth Dec 26 '13 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)
mP.
  • 18,002
  • 10
  • 71
  • 105