File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Question on Overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Question on Overriding" Watch "Question on Overriding" New topic
Author

Question on Overriding

chandru ram
Greenhorn

Joined: Jul 03, 2002
Posts: 18
Hi All,
I tried the following example
class A {
public void print(int a) {
System.out.println("method from class A accepting int: "+a);
}
public void print(long a) {
System.out.println("method from class A accepting long: "+a);
}
}
class B extends A {
public void print(long a) {
System.out.println("method from class B accepting long: "+a);
}

}
public class Test {
public static void main(String args[]) {
B b = new B();
b.print(5);
}
}
When this program is compiled, there is a compilation error like :
Test.java:23: reference to print is ambiguous, both method print(int) in A and method print(long) in B match
b.print(5);
^
1 error

When we try to do this operation base class alone like A a = new A(); and then a.print(5).it works correctly. the compiler associates the call to the method accepting int as parameter.
but why this doesn't happen when the method is over-ridden?
pls explain.
Thanks in advance,
chandru
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
I was puzzled, so I copied the example and tried to compile and run it. Guess what? It worked fine, giving me "method from class A accepting int: 5".
I'm running JDK 1.4.2_x on Win2000. What version of the JDK are you using? It may have been a problem in an earlier version of Java which is now fixed ...
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

I believe that it is the case that is is no longer a problem with the current JVM. I think that this same question was asked a few weeks back Marilyn posted a link to a change in the JLS that describes this situation; I can't find that link right now.
Basically, a slight change was make to the rules governing what is more "specific" than what, and now the rules work (more like) what you would expect.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Sandeep Jindal
Ranch Hand

Joined: Aug 25, 2003
Posts: 180
Dear Wayne,
Like you, I was also puzzled and tried the example. When i tried, i got the same compile error as Mr Chandru got. And I want to add that i m ruuning JDK1.4.1 on win2K.
So does this mean that the problem is resolved in 1.4.2?
And if it is, then why was the compiler giving error before that? I mean at what point the compiler was confued?

Regards
Sandeep


SCJP 5.0
http://sites.google.com/site/duddlutechnologies/home
chandru ram
Greenhorn

Joined: Jul 03, 2002
Posts: 18
yes this is a bug with previous releases. please see this link for more details:
http://www.sys-con.com/story/?storyid=34292

Regards,
Chandru ram
 
jQuery in Action, 2nd edition
 
subject: Question on Overriding