• 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

Overloading var-args and array

 
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We cannot overload a method that is taking var args as parameter with a method thgat is taking array as a parameter. as shown in below example. is from Devaka's Practice Exam2 Q:No18. And I tried the same method with overridding . It is showing a warning that a varargs method should overridde other varargs method. apart from that any importanat points from this???





 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Important point: Both method signature are considered same, so you cannot overload, but you can override.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

var-args and array argument declarations are not completely equivalent, only var-args convert internally to array, not vice-versa. For the problem above (overriding/overloading) this does not matter. But if we vary the main method (leaving the remaining part above unchanged) we see differences:

compiles;


compiles not.

 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John wrote: For the problem above (overriding/overloading) this does not matter.



What you want to say? For the problem above overloading does not matter ?? Give me an example for overloading where var-args and array does not matter.
 
John Grabowsky
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

slow down. The "problem above" is overloading and overriding. When considering overriding and overloading, both method signatures can be considered same, so your post was perfect. The only point i wanted to add is, that there are situations (e.g. when calling the method as in my example), where the method signatures are not equivalent. This non-equivalence does not matter when considering overloading and overriding.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried John's code, and strangely enough this compiles:


But modify the code by removing the extends VarargsEx from VarargsEx1, and the code doesn't compile. I thought I would point it out for completeness. I don't understand very well what is going on.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ruben,

Your code does not compile because VarargsEx1.printFirst takes an array of integers and v.printFirst(5, 2, 3); does not pass an array but passes 3 integer parameters. So the compiler is right.

Varargs methods should only override or be overridden by other varargs methods. Else we'll get a compile time warning. Now if we execute this code -



we get a compile time error at new Test().printFirst(2); because the var-arg method is now acting as a int[] method.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vijay,

Actually, both my code and your code are compiling and running fine for me. This is my java -version output:

java version "1.6.0_07"
Java(TM) SE Runtime Environment (build 1.6.0_07-b06)
Java HotSpot(TM) Client VM (build 10.0-b23, mixed mode, sharing)

And my javac -version output:
javac 1.6.0_07

What version of java and javac do you have?

Thanks for your help.
 
Vijay Raj
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ruben,

Sorry at my part, I did not see that VarargsEx1 extends VarargsEx. But still, I feel it should not compile. I got to go and check the books.

For VarargsEx1, the printFirst(int ...) in VarargsEx will look like printFirst(int[]) and thus the call v.printFirst(5, 2, 3); should throw a compile time error.

Vijay.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vijay,

The fact that it compiles tells me that there is something going on. Perhaps when you override a var-args method with the similar non-var-args method, the overriding method just gets the var-args signature. That would explain things.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic