Name: boT120536 Date: 01/22/2001
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)
Below an example where jdk1.2 does not compile and jdk1.3 does, but the
execution seems to be incorrect : a virtual call do not apply...
3 classes in 3 different packages : C inheriting from B inheriting from A.
- a protected method foo() is defined in A and redefined in C
- method precall() defined in B calls foo() directly and through an inner
classes (Caller)
In jdk1.2, compile error due to foo() call in the inner class (setting foo()
public works fine).
In jdk1.3, no compile error, but calling precall() from a C instance really
calls C.foo() in a direct invocation, but execute A.foo() in the inner
classes !!??
see the log :
c.precall()
C.foo() on c.C@3179c3
A.foo() on c.C@3179c3
Moreover, if we redefine foo() in class B too, we get:
c.precall()
C.foo() on c.C@3179c3
C.foo() on c.C@3179c3
Amazing no ?
----------------
package a;
public class A {
protected void foo() {
System.out.println(" A.foo() on "+this);
}
}
----------------
package b;
import a.*;
public class B extends A {
interface Caller {
void call();
}
public void precall() {
foo();
Caller caller = new Caller() {
public void call() {
foo();
}
};
caller.call();
}
/*
protected void foo() {
System.out.println(" B.foo() on "+this);
}
*/
}
----------------
package c;
import b.*;
public class C extends B {
protected void foo() {
System.out.println(" C.foo() on "+this);
}
}
----------------
import c.*;
class MyTest {
static public void main(String args[]) {
C c = new C();
System.out.println("c.precall()");
c.precall();
}
}
(Review ID: 110513)
======================================================================
- duplicates
-
JDK-4683294 Inner class calls wrong version of inherited protected method
- Closed