Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is method overloading a form of polymorphism or something else?

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?

like image 412
crowso Avatar asked Dec 09 '22 23:12

crowso


1 Answers

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.

like image 98
Tomasz Nurkiewicz Avatar answered Dec 14 '22 22:12

Tomasz Nurkiewicz