How can the wrong override method be called through multiple levels of inheritance?
I have the following Java class structure. (JDK 1.6.0_21)
Class C extends B` {
//Nothing relevant
}
Class B extends A {
public void clear() {
// whatever
}
}
Class A {
public void clear() {
// whatever
}
}
I had an object c of type C. When I was calling c.clear()
, it was calling the clear()
method from class A
, not class B
. After more than a few clean and builds, it started using the clear method on class B
, which is as expected.
Before anyone states "You musn't have 开发者_如何学Gorebuilt after changing class A, B or C". These classes haven't changed in 3 years. I even added debug lines to prove it was calling A.clear()
, which also proves that it had all been recompiled.
Does anyone know of any reason it would have been calling A.clear()
instead of B.clear()
, remembering that all three of these classes haven't changed in years?
EDIT: Class A and B are part of a library that end up in a jar file. Class C
is in a Java Application. Both these projects are checked out in Eclipse and were compiled multiple times with this issue - as I was adding debug in the library and debug in the Java app.
EDIT AGAIN: The clear()
method in Class B
does NOT use super.clear()
, and neither is it supposed to.
FINAL EDIT: The answer has to be that there was some kind of compilation/environment issue - if Java's JDK had some kind of flaw effecting the inheritance model there would be much more heard of it. Without exposing the entirety of the project, I don't think there's much more anyone can do. Thanks to those who answered!
I even added debug lines to prove it was calling A.clear(), which also proves that it had all been recompiled.
No it doesn't. The debugger can run with different code than the one deployed. You'll just be seeing the wrong lines, and in the worst case - the same lines (if the code changes didn't move anything around)
Next time make sure the date of the files/jars is not in the past.
精彩评论