• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method overload doubt

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why there is problem in below program to compile,why it can't have these two overloaded method together here


 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is because, var args is nothing but an array of arguments.
e.g. 'int... args' is an array of int.
So, here, you are having two methods, with same name, same return type and same arguments - which is not method overloading, but it is duplicate method definition and hence the compile time error.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because int[] and int... refer to the same type int[] and hence compiler gives an error. For overloading you should have the parameter list different.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote:This is because, var args is nothing but an array of arguments.
e.g. 'int... args' is an array of int.
So, here, you are having two methods, with same name, same return type and same arguments - which is not method overloading, but it is duplicate method definition and hence the compile time error.


Both are not same if they are then second program should work with same argument.



Output:- succeed

Now watch


Output:-compile error
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The 'arguement-to-array' conversion takes place internally. The principle behind var-arg is that you can invoke the method with one or more arguments of a specific type, and within the method, all those arguments will be treated as an array.
Second code fails because you are passing a single int whereas method expects an int array.
First code works because the argument(s) are taken and further those are placed into an int array before passing to the method. You can further verify this by doing array specific operations in first code.
e.g. when you have 'int... args' as argument, and you invoke the method by passing a single int, then that int is assigned to arg[0], not to arg.
This is the reason why you cannot do int specific operations on doArgs in first code (e.g. doArgs++ will give you compile time error), and you can do array specific operations on doArgs there (e.g. printing doArgs.length etc.)
I hope this helps.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It (almost)coincides with my and John Jai's statements
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic