Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Varargs behaves differently in method overriding and in normal method?

 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was studying varargs so found one of the links which baffles me varargs overriding old topic link
Example 1:Copied code from the above link
Output:
1. (String) => (String, Number[])
2. (String, int) => (String, Number[])
3. (String, Integer) => (String, Number[])
5. (String, int, int) => (String, Number[])
7. (String, int, int, int) => (String, Number[])

Example 2:Same way another example I tried with
Output:
Size of y is: 0
(String) invokes  methodOne(String str, Integer... y)
***********************************
Size of y is: 1
(String,int) invokes  methodOne(String str, Integer... y)
***********************************
Size of y is: 1
(String,Integer) invokes  methodOne(String str, Integer... y)
***********************************
Size of y is: 3
(String,int,int,int) invokes  methodOne(String str, Integer... y)
***********************************
Size of y is: 4
(String,[]int) invokes  methodOne(String str, int[]y)
***********************************

My confusion is why in example 1 it calls doIt(String str, Number... data) but not doIt(String str, Integer... data)? where in example 2 it calls methodOne(String str, Integer... y) but not methodOne(String str, Number... y) as example 1 do?
 
Sheriff
Posts: 22573
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ganish Patil wrote:My confusion is why in example 1 it calls doIt(String str, Number... data) but not doIt(String str, Integer... data)? where in example 2 it calls methodOne(String str, Integer... y) but not methodOne(String str, Number... y) as example 1 do?


For every set of overloaded methods, the compiler will use the most specific one (or cause a compiler error if there's ambiguity). If you'd use an instance of OneSuperclass and pass it an Integer, it would use the Integer varargs method, because Integer is more specific than Number.

However, in OneSubclass, the Integer varargs method no longer exists, because you've overridden it with a version that uses an explicit array. The class now has three doIt methods:
* one takes Integer[]
* one takes Number...
* one takes Object...

If you pass the Integer values as varargs, the most specific method is the Number... one, because a single Integer does not match Integer[].


In your second example, there are three varargs methods, and so the most specific one is the Integer... one.



Now, if your override in OneSubclass would also have used Integer... instead of Integer[], you' see the same behavior as the second example.
 
Greenhorn
Posts: 22
Oracle Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice explanation
 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:For every set of overloaded methods, the compiler will use the most specific one

Ohh I see,

In your second example, there are three varargs methods, and so the most specific one is the Integer... one.

Most specific method is having Integer then after that would be Number then Object. am I correct?

Now, if your override in OneSubclass would also have used Integer... instead of Integer[], you' see the same behavior as the second example.

Yes I got that now, It was too confusing but your explanation cleared it. Thank you so much  
 
Rob Spoor
Sheriff
Posts: 22573
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ganish Patil wrote:

In your second example, there are three varargs methods, and so the most specific one is the Integer... one.

Most specific method is having Integer then after that would be Number then Object. am I correct?


Correct
 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Rob  
 
Rob Spoor
Sheriff
Posts: 22573
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
reply
    Bookmark Topic Watch Topic
  • New Topic