Win a copy of Fixing your Scrum this week in the Agile 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • 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.
 
Life just hasn't been the same since the volcano erupted and now the air is full of tiny ads.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic