[3747] in java-interest
Re: Fundamental flaw in Java library distribution scheme
daemon@ATHENA.MIT.EDU (Jim Graham)
Sat Nov 25 05:53:49 1995
Date: Sat, 25 Nov 1995 01:40:54 -0800
From: flar@bendenweyr.Eng.Sun.COM (Jim Graham)
To: perry@cdt.luth.se
Cc: java-interest@java.Eng.Sun.COM
Hi Perry,
> David points out that if you change class C to include an
> equals(C obj) method, but only recompile the C class, L will
> still call the inherited equals(Object obj) from Object even if
> the argument is a C object. That is until class L is recompiled
> as well.
Class L might call the C.equals(C obj) method, or it might call
the Object.equals(Object obj) method. It depends on how the
arguments are cast in the specific call. In particular, if you
call:
public void foo(C target, C obj, Object obj2) {
target.equals((Object) obj); // Case 1
target.equals(obj2); // Case 2
}
Then the Object.equals(Object obj) method will be called in both cases,
bypassing the C.equals(C obj) even if both obj arguments happen to be
of class C, because in the first case this method explicitly used
casting to choose the method it wanted, and in the second case the
compile time type of obj2 can only proven to be as specific as
"Object".
This is how you can affect the compile time choice for which overloaded
operator will be used, by controlling the visible types of the
arguments in the line containing the method call. On the other hand,
without the cast, then yes, when class L is recompiled it will switch
from using Object.equals() to C.equals() for the first case since that
new method will be determined to be more specific.
> This is not wrong. Class L can't know that there now is a new
> method in class C. Think about it. If you add a method to a class
> C, wouldn't it be strange if classes using that class, suddenly
> started calling this new method, without even being recompiled!?
The beauty of Java is that if you override a method, which involves
using the same arguments as the inherited method exactly, then the
new method *will* get called automatically at runtime *without*
recompilation!
But, in order to get the benefit of this dynamic dispatch of the
overriden method, you must use exactly the same argument types
and return type in the method as the inherited method. That is
the distinction that seems to be misunderstood in this discussion.
...jim
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com