Thursday 19 September 2013

would like to know why the super class method is called

would like to know why the super class method is called

class One {
public void doThing(One o) {System.out.println("One");}
}
class Two extends One{
public void doThing(Two t) {System.out.println("Two");}
}
public class Ugly {
public static void main(String[] args) {
Two t = new Two();
One o = t;
o.doThing(new Two());
}
}
I know that at runtime, even though the object reference is of the super
class type, the actual object type will be known and the actual object's
method will be called. But if that is the case, then on runtime the
doThing(Two t) method should be called but instead the super class method
doThing(One o) is called. I would be glad if somebody explained it

No comments:

Post a Comment