This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Simran, the Child class doesn't inherit the say method as you are saying. Lets see an example
Now if the Child class inherits say as you are saying, then what will happen at runtime?? The statement String m = message; will try to assign an int to a String. So basically since the say method is in Parent class, access to instance fields from that method will always point to instance fields of Parent class. The rule is, access to methods is polymorphic but access to fields is not polymorphic...
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.