2

Wikipedia states that there are 3 (major) types of Polymorphism. Correlating this definition with my limited understanding of Java Polymorphism, this is the mapping I came up with -

  1. Adhoc polymorphism - Java Function Overloading (dynamic dispatch).
  2. Parametric polymorphism - Java Generics.
  3. Subtyping - Java Function Overriding (static dispatch).

Is this correct?

References - 1. https://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29 2. Polymorphism - Define In Just Two Sentences

Community
  • 1
  • 1
Quest Monger
  • 8,252
  • 11
  • 37
  • 43

2 Answers2

1
  1. Ad-hoc polymorphism would map to method overloading (where the method is already known at compile time)
  2. is correct

  3. Subtyping would map to to overriding (dynamic dispatch)

user140547
  • 7,750
  • 3
  • 28
  • 80
  • 1
    Subtyping is not restricted to functions/methods. It's more a fundamental property of Java's OO paradigma itself: ["If S is a subtype of T... any term of type S can be safely used in a context where a term of type T is expected"](https://en.wikipedia.org/wiki/Subtyping). – JimmyB Jul 02 '15 at 08:27
  • 1
    I have to disagree with your interpretation of 1. and 3. - Ad-hoc polymorphism [maps to *overloading*](https://en.wikipedia.org/wiki/Ad_hoc_polymorphism), not overriding which is just *not* known at compile time. And subtyping is just the way Java enforces it's type safety in general, see my other comment. – JimmyB Jul 02 '15 at 08:30
  • yes I have really confused them, sorry, quite embarrassing, I would have deserved a downvote as well :S – user140547 Jul 02 '15 at 08:30
1
  1. correct.
  2. correct.
  3. Subtyping - In general, that's how Java's inheritance works, irrespective of methods or fields or parameter types.
JimmyB
  • 12,101
  • 2
  • 28
  • 44