Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

primitive Coversion in methods

 
Kishan Kumar
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Everybody here are doing a splended job. Thanks to Internet.
Please see the code below,

I have defined two methods one takes int and another takes long.
There is no problem here and output is
Int Version : 10
But when i put one of the methods in another class and do a inheritance, see code below

This is giving compile time error
Reference to method is ambigious, It is defined in void method(long) and void method(int).
This ambiguity should have arised in the earlier code itself but
it is rightly taking the int method. Why is that, problem arises
only in inheritance. It is a instance of overloading.
Also if I interchange the method declerations and change it as
public void method(long i) in the superclass call and
public void method(int i) in the subclass callsub
It is able to give the output
Int Version : 10
Also I am not able to call the int method in the superclass at all from the subclass.
Can you folks please explain this behaviour.
Your help is highly appreciated.
[I added UBB CODE tags to your source code to make it more readable. Please try to use them in the future. Learn more about UBB codes - Ajith]

[This message has been edited by Ajith Kallambella (edited September 26, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all !
I think this is an interesting question. Can someone come up with an explanatiopn for this.
All I can tell is:
For overloading in a class, the most specific method is choosen.
But with the 2nd case, it'd be nice if someone came with an explanation.
-sampaths
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When determining if there is a maximally specific method, the compiler uses not only the types of the arguments, but also the type of the definer
In the second example above, the versions of method defined in call and callSub are both
maximally specific. 'call's method is not more specific than 'callsub's because the class 'call' cannot be converted to the class 'callsub' by method invocation conversion. callsub's method is not more specific than 'call's because the type long cannot be converted to int by method invocation conversion. Since there is more than one maximally specific method, according JLS section 15.12.2.2 Choose the Most Specific Method we have an ambiguity.
Look at these two posts in Sun's bug parade for more detailed explanation -
Note 4038412
Note 4067106
Hope this helps,
Ajith
 
Kishan Kumar
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ajith for the reply.
But My brain could not catch hold the line
"'call's method is not more specific than 'callsub's because the class 'call' cannot be converted to the class 'callsub' by method invocation conversion"
If possible can you explain me more about that.
I get the point that the subclass mthods should be more specific
than the superclass methods. Am I right?
Thanks for your time.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic