Abimaran Kugathasan wrote:Ankit Garg said,
but access to fields is not polymorphic...
Then what is that? Can give the exact answer.
Here the variable 'message' inherited downward to Child class???,,,,
Polymorphism generally means that there is a mechanism that determines the latest version of a method (or variable, if supported) at runtime. In the case of
Java, methods go through a jump table to make sure that the latest version is called.
For static methods (or field variables) which doesn't support polymorphism, this mechanism doesn't exist. Instead, the target is determined at compile time. In the case of your latest example... yes, the compiler is smart enough to realize that the field doesn't exist in the child (when compiling the child class), and hence, uses the parent instead. So, it does "look" sortof like polymorphism...
But it isn't. Fields are not inherited. And the compiler will not get this right all the time. As shown in your first example.
Henry