Why does java allow overloading? One class with 10 methods of same name would be a strain on the compiler and it also fosters bad programming practices.
Asked
Active
Viewed 148 times
-11
-
6"*One class with 10 methods of same name would be a strain on the compiler*" they don't have the same signature and the overloading rules are very well defined, so not really... – assylias Dec 10 '12 at 08:22
-
6Where do you get these ideas from? – Andrew Logvinov Dec 10 '12 at 08:22
-
You're not smarter than java creators. Search in google and you'll get your answer quickly – shift66 Dec 10 '12 at 08:22
-
possible duplicate of [Method Overloading. Can you overuse it?](http://stackoverflow.com/questions/248222/method-overloading-can-you-overuse-it) – assylias Dec 10 '12 at 08:23
-
1This is surely subjective/argumentative. – Jacob Stanley Dec 10 '12 at 08:23
-
1000 methods with the same name might take a dent in the compiler speed, but not 10. You will hit the hard limit on method count before the compilation speed becomes unbearable. – John Dvorak Dec 10 '12 at 08:24
-
1000 methods with different names probably makes a decent dent in compiler speed as well :) – Jacob Stanley Dec 10 '12 at 08:25
-
As you are clearly not a compiler writer it is difficult to understand why you are pronouncing on a topic you know nothing about. – user207421 Dec 10 '12 at 08:47
-
The speed of the compilation is rarely an issue. Writing simple clear code is important but it's not the compiler's job to prevent every possible thing which is not simple and clear. For example, methods are limited to 64KB, more than enough you would think except if you are generating code. – Peter Lawrey Dec 10 '12 at 09:48
2 Answers
3
the java compiler uses the message signature (name, accepted parameters and types and to some degrees the return type) not the method name to identify the method, so there is no additional strain to the compiler. If two methods achieve the same result and differ only in the parameters to complete its job, why should one not give the two methods the same name? It makes your code more readable this way.
-
I thought the compiler used the method name alone but I realised now that for any method name when we dont pass the right arguments it throws a error. So everything is used as a combination. So even if there are 10 methods with same name it doesnt really matter.Thanks a lot – mavrav Dec 10 '12 at 08:37
-
1
The main advantage is the cleanliness of code. Otherwise you will have many methods doing similar operations with different names.
You can also read this document for its other advantages.

Parvin Gasimzade
- 25,180
- 8
- 56
- 83