Sunday, 18 August 2013

Why superclass invokes the subclass's overrided method?

Why superclass invokes the subclass's overrided method?

I have two classes, Parent (superclass) and Child (subclass).
public class Parent(){
public void hello(){
System.out.println("HelloParent");
bye();
}
public void bye(){
System.out.println("ByeParent");
}
}
public class Child extends Parent(){
@Override
public void hello(){
super.hello();
}
@Override
public void bye(){
System.out.println("ByeChild");
}
}
If I create a Child instance and I invoke its hello() method, it invokes
the Child's hello method, and not the Parent's hello method.
Child c = new Child();
c.hello();
It writes: "HelloParent" "ByeChild"
But why is not "ByeParent"? Why does not the superclass's method invoke?

No comments:

Post a Comment