Originally posted by Mike Beaty:
If the type of object reference determines what variables are accessed, and the underlying object determines what methods are accesed, why doesn't the child's method get called when you say:
d = c;
d.method();
???
Wouldn't that mean that for d, the Parent variables are accessed, and Child's methods are used?
Yes, this is true, BUT, look at the Parent's declaration of method(). It is
PRIVATE .
That means, it is NOT inherited by any subclasses. When the compiler is compiling this line of code:
d.method();
It first looks at the reference type of d to see if it really has a method named method() that is accessible. Now, because you are calling this method within the class (main is in the class, and so has access to private members) it is legal for you to access private members of d. BUT, because the type of d is a Parent, and the method() is private, the compiler KNOWS that this method cannot be overridden in any possible sublcasses of Parent, since it's private. SO, it doesn't have to create bytecode for runtime binding; it creates a fixed, static call to the method() in the Parent class.
IF this method had not been private, then it would have been inherited by subclasses of Parent, so the compiler would have had to create bytecode to dynamically bind this method call at runtime. But that is not the situation here.
Rob
