Arathi<br />Sun Certified Java Programmer
Arathi<br />Sun Certified Java Programmer
Originally posted by Valentin Crettaz:
This is something that I will explain in an article that will be published in the February newsletter.
But here goes.
Declaration:
Base o = new Over();
The compile-time type of o is Base and the runtime type of o will be Over. The method to be invoked MUST be defined in class Over since the method will be invoked at runtime on o (which is of type Over). But since the compile-time type of o is Base, the latter MUST also declare the method. If that method (in Base) is declared private then we say that the invocation mode is non-virtual (which means in the JLS jargon that the method cannot be overridden by subclasses).
Now, if you declare the same method in Over (that is you override the method of Base) then the compiler is going to choke on it because overriding is not allowed.
The difference between
Over o = new Over();
and
Base o = new Over();
is that in the first case everything will be fine because the method amethod will be searched in Over both at compile and runtime, while in the second case, Base will be searched at compile-time and Over will be searched at runtime. The compile-time search will fail since the method amethod is private in Base.
I don't know if it clears things up... Let us know
Consider Paul's rocket mass heater. |