#839 Problem with polymorphism in interactions

2: Annoying
open-accepted
nobody
7
2010-03-03
2010-03-03
No

// 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)

Discussion

  • Mathias Ricken

    Mathias Ricken - 2010-03-03

    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
    >

     
  • Mathias Ricken

    Mathias Ricken - 2010-03-03
    • priority: 5 --> 7
    • status: open --> open-accepted
     
  • Dan Smith

    Dan Smith - 2010-04-11

    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.

     
  • Mathias Ricken

    Mathias Ricken - 2010-08-10

    Still present as of revision 5353.

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks