• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Method overload doubt

 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:confused:


 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It (almost)coincides with my and John Jai's statements
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic