All, I don't get one of the quiz problems -- the code is pasted below, and the result is "super super super super base"
My question is: Object b is of type base, even though it has a superclass-reference. By the definition of
polymorphism, the print() method of base should be invoked. Why in this case, it was that of superclass i/o base? Isn't that a violation of polymorphism?
Thanks
public class overwrite {
public static void main(
String[] args) {
superclass a = new superclass();
superclass b = new base();
base c = new derived();
a.print(new base());
b.print(new derived());
b.print(new base());
b.print(new superclass());
c.print(new derived());
}
}
class superclass{
void print(superclass s){
System.out.println("super");
}
}
class base extends superclass{
void print(base b){
System.out.println("base");
}
}
class derived extends base{
static void print(derived d) {
System.out.println("derive");
}
}
