Win a copy of Microservices Testing (Live Project) this week in the Spring 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:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Overloading question

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This code compiles fine and prints out "array". Is the method declaration "void method(Object[] o)" more specific than "void method(Object o)"?
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeeny,
whenever a overloaded method was called with a parameter that can be assaignable to more than one overloaded method ,
then the method whose parameter(s) are assaignable to other method parameter type will be called.
In your example u can always assaign Object[]o type to the reference of type Object(i.e Object o in your case)
Hope u got it.
rishi
 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All
I still do not understand why "array" is printed out..
Could some help please?
Thanks,
Cathy.
 
Jeeny Dorph
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx, Rishi!
I got it
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cathy
The point is that Object's array is assignable to Object because an array is also an Object. But the reverse is not true. That is there is no gaurantee that an array will always be an Object. Hence array is printed.
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Anupam did u mean an array can be of primitive type true ..the reverse cannot be true..
thanx.
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did some experimenting and it seems like the compiler is working very hard to find the most specific method that could possible work. I amplified the example a bit.As is the compiler complains about not being able to decide between Integer[] and Float[]. But if line 1 is commented out, "object array" prints. If line 1 is uncommented and line 2 is commented, "integer array" and "object array" prints.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic