File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes primitive Coversion in methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "primitive Coversion in methods" Watch "primitive Coversion in methods" New topic

primitive Coversion in methods

Kishan Kumar
Ranch Hand

Joined: Sep 26, 2000
Posts: 130
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).]

Regards,<BR>V. Kishan Kumar
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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.
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782

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 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,

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Kishan Kumar
Ranch Hand

Joined: Sep 26, 2000
Posts: 130
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.
I agree. Here's the link:
subject: primitive Coversion in methods
It's not a secret anymore!