// Dr. Java r5122 and r5004 jar-Files, Windows 7
// in interactions window enter the following code:
class A {
int x() { return 1; }
}
class B extends A {
int x() { return 2; }
}
A a = new B();
a.x()
// result is:
1
// this is wrong, it should be 2 (polymorphism for non-static method)
Verified in the latest weekly build. Note that this only happens if classes are defined in the Interactions Pane.
// in Definitions Pane
public class PolymorphismBug {
public static class A {
public int x() { return 1; }
}
public static class B extends A {
public int x() { return 2; }
}
public static void main(String[] args) {
A a = new B();
System.out.println(a.x());
}
}
// in Interactions Pane
Welcome to DrJava. Working directory is D:\Documents\drjava\test
> java PolymorphismBug
2
// correct
> PolymorphismBug.A a = new PolymorphismBug.B()
> a.x()
2
// also correct
> class A {
int x() { return 1; }
}
class B extends A {
int x() { return 2; }
}
A a = new B();
a.x()
1
// wrong
>
As a workaround, protected and public methods behave correctly. (Private methods also behave correctly -- they are not inherited/overridden.) A bug only occurs when the method is package-private.
There must be something wrong with the DynamicJava-compiled classes, but I don't see any relevant difference in the javap output (compared to javac-compiled classes). I'll investigate further.
Still present as of revision 5353.