#29 polymorphic methods - runtime vs. declared types

open-later
nobody
General (151)
2
2001-05-02
2001-03-27
Allan Crooks
No

Hi,

The following piece of code should illustrate the
problem. Both invocations of test print out "I am an
integer". For the o1 case, this is not too important
(even though it means that BeanShell is not consistent
to Java in that respect), but the o2 case is probably
more important than the first.

-- CLASS DEFINITION --

public class Test {
public void test (Object o)
{System.out.println ("I am a generic object.");}
public void test (Integer f)
{System.out.println ("I am an integer.");}
}

-- EXECUTION SCRIPT --

Object o1 = new Integer (5);
Object o2 = null;
new Test().test(o1);
new Test().test(o2);

Allan.

Discussion

  • Pat Niemeyer
    Pat Niemeyer
    2001-05-02

    Logged In: YES
    user_id=18885

    The second issue involving the null argumetn is bug #213066.

    The first issue is an open question... We are behaving in a
    more dynamic way than Java by using the run time type for
    overloaded method resolution. We should probably use the
    declared type when available for the sake of Java
    compatability, even though the behavior isn't really wrong.

     
  • Pat Niemeyer
    Pat Niemeyer
    2001-05-02

    • priority: 5 --> 2
    • summary: Problems with polymorphic methods --> polymorphic methods - runtime vs. declared types
    • status: open --> open-later