4

I have a long standing doubt. Could someone please tell me whether method overloading is a form of polymorphism or is it something completely different?

dgund
  • 3,459
  • 4
  • 39
  • 64
crowso
  • 2,049
  • 9
  • 33
  • 38
  • possible duplicate of [Is Method Overloading considered polymorphism?](http://stackoverflow.com/questions/2400284/is-method-overloading-considered-polymorphism) – Toon Krijthe Jun 17 '12 at 00:25

5 Answers5

7

Method overloading is just a syntax sugar allowing you to have methods with same name but different arguments. It has nothing to do with polymorphism. Method overloading is typically used to define two methods accepting different arguments, e.g.:

public void println(boolean x) //...
public void println(char x) //...

or to skip certain parameters and use some defaults:

public String substring(int beginIndex) //...
public String substring(int beginIndex, int endIndex) //...

Method overriding, on the other hand, is a foundation of inheritance and is more closely related to polymorphism.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
3

Polymorphism, literally means something which has multiple behavior.

In java, we can have a static and runtime polymorphism.

Overloading is static polymorphism since it allows different behavior by means of passing varying arguments. But this is resolved at the complile time only, hence static.

Overriding, is dynamic polymorphism since the actual call to the function depends on the type of object invoking it which is available only at the runtime, hence dynamic.

Nrj
  • 6,723
  • 7
  • 46
  • 58
  • so you are contradicting all the above comments ? – crowso Jun 17 '12 at 03:17
  • 1
    @user581544 : its my opinion and what i have read in past years.. Frankly I do relate both of above to polymorphic behavior.. – Nrj Jun 17 '12 at 15:53
2

No, it is not.

With overloading you just provide different implementations of a same method name with different signatures.

Since polymorphism (by subtyping) requires the same signature (that is made either by method name either by parameters) then the two things can never intersect.

Jack
  • 131,802
  • 30
  • 241
  • 343
1

No, its not, it's method overloading.

java does polymorphism via interfaces. It has no multiple inheritance.

You can, however, simulate multiple inhertance by using multiple interface and the composite/delegate pattern.

Polygnome
  • 7,639
  • 2
  • 37
  • 57
1

No it's not related to object oriented programming. Overloading simply means that you can use the same name for different method signatures.

tskuzzy
  • 35,812
  • 14
  • 73
  • 140