• 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

Selecting most specific method

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


Output is Int.

As per the theory "if the parameters of Method A are valid in Method B, but not vice versa, then the compiler choose Method A" The answer should be "Byte" but its not.

What is the reason behind? Please help me with this.

Thanks
[ March 03, 2008: Message edited by: sandhi mridul ]
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case we cannot say that parameters in <I>Test6(1,2)</I> are valid for all three constructors. This is because 1, 2 are int values and only the constructor Test6(int i, int j) is accepting int. The constructors Test6(short i, short j) and Test6(byte i, byte j) are not accepting int values. For each constructor to execute, the values which are being passed should be explicitly casted in to short or byte accordingly as follows;

Test6(1,2) --> Test6(int i, int j)
Test6((short)1,(short)2) --> Test6(short i, short j)
Test6((byte)1,(byte)2) --> Test6(byte i, byte j)


Try to execute the same code without the constructor Test6(int i, int j), it gives an error asking for the removed type of a constructor with int arguments. This clears the fact that the other two available constructors are not capable of handling (1,2) which are int parameters.

Try the variations of the above code and you will get a clear idea of how things work.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic