Gary Marshall filed this complaint:
Dynamic binding is all well and good, but I need to get a clear understanding of what that compiler understands before I take the exam.
And so far I'm not understanding why the compiler doesn't see that "b" is a new "Sub"?
..... Base b=new Sub();
Is it the rule that the compiler only looks at the declared type to the left of the equals sign?
Yes. With your snippet, it looks as if the compiler is a bit lazy because it does not look at the object. But in your example there is only one static line, this does not necessaryly have to be so.
E.g.
Here, only at runtime it is known, if the Number is a Double or an Integer.
The compiler has no "sense" of this runtime types.
Generally speaking, if you have
Base b=new Sub(); Then only Base-methods can be invoked with variable b.
If Sub has new own methods that Base does not have, you cannot invoke them with variable b. However, you could downcast
Sub mySub = (Sub) b;
mySub.subSpecific_method();
Perhaps see also the
thread with the
coconut.
Yours,
Bu.
[ September 27, 2007: Message edited by: Burkhard Hassel ]