Joe Parks
Posts: 107
Nickname: joeparks
Registered: Aug, 2003
|
|
Re: Help me better Understand?
|
Posted: Oct 20, 2003 4:42 AM
|
|
> When you invoke a method through an object reference, > the actual class of the object governs which > implementation is used.
This implies that the reference type and the actual class share the same public interface, but differ in implementation.
If you were to add an overriding "print" method to your Child class,
public void print() {
System.out.println("Child");
}
Then you could see the point of this section of the spec:
public static void main(String [] args) {
Parent grandparent = new Parent();
grandparent.print(); // will print "Parent"
Parent parent = new Child();
parent.print(); // will print "Child"
}
Because (in this example) parent is declared as type "Parent", you cannot invoke methods on it that do not exist in the Parent public interface without casting it.
(Child)parent.col();
|
|