Tuesday, October 23, 2007

Inheritence and PolyMorphIsm


Don't believe in any 'ISM' but making an exception here, perhaps the SUN guys will
change the word some time later as all 'ISM' brings bad news in the end. Maybe,
PolyMorphISM is no exception to this 'invariant'!

For the following discussion, we have to classes, Super and Sub which extends the 
Super class, both class defines the same non-static method (same signature, return 
type) within itself, we call this method methodx

Super A = new Sub ();
A.methodx();

Calls the methodx of the Sub class, _not_ the Super class.
This is dynamic binding at work. Most important thing to remember is that Dynamic 
Type is the KING!
Doesn’t matter what the static Type is! The method to call at run time WILL BE 
based on the dynamic type.
Thus in the above, when we do the method call, the ‘methodx’ of the Sub class will 
be called even though static type is Super. But we don’t care about static type with 
dynamic binding.

Sub B = new Super ();

This results in compile time error, since we can not go from low to high in terms of 
assignment. The way I put it is that the servant can not wear the Master’s shoes! 
(At least according to Java Compiler). If the Super class is the Master and the Sub
class is the slave, then Sub class can not own property titles of the Master. Here 
we extend the analogy this way....the Super class is the Master and objects of that
class are the property of the Master. The static type of the Parent class is 
actually the title deeds on the property. So, here we are thinking that, in terms of
references, or arrows, those are actually property titles and the actual property is 
the actual instantiated objects, while the Master is actually the Class itself. Ok…
maybe convoluted. But stay with me, I’m making this up as we go along! :) In any 
case, in terms of the analogy, Static type Sub is the slave and all variables of its 
type (like B) can only be of deeds to its own property (meager though they maybe or 
not…after all can slaves really own ANYTHING? They can not, only they seem to do so
some times, which is an illusion. This hypothesis will be verified and testified by
Java compiler in a little while as we will see!), i.e. instantiated objects of its 
own type (Sub class type). If it tries otherwise, Java will stop it. Thus we will 
get a compile error.

Super C = new Super ();
((Sub) C).methodx();

This results in a run time error. Here we have the Master and all, but we try to 
cast the master as a slave. This passes through compiler, as they are all beings of
the same world. However, when running it will found that the dynamic type is also 
Super so, the compiler doesn’t know what to do in such a situation. Going back to 
the Master slave analogy, we have a proper Master here with both static and dynamic 
type as Master, thus when we ask it to do slave work, it says, “I don’t do no slave 
work!” (you can see how the Master picks up some slave idioms in the in the mean 
time…)

However if the casting was like this…

Super D= new Sub ( );
 ((Sub) D).methodx ( );

The everything is OK, as then the Master is cast into slave and it was a slave all
along! The dynamic type was Sub! We don’t even have to cast…as D was a slave by it’s 
dynamic type all along, and we know that dynamic type RULES!!!



Sub E = new Sub ();
((Super) E).methodx();

Here still, same stuff, just have to remember that dynamic type is what matters. We 
can cast the slave E to be the of the Master race but when he goes to work, we see 
that all he knows is slave work and none of the Master’s work. Thus, the methodx 
from the Sub class gets called. Since even though we case E, we just change the 
Static type by the cast, we can not change the Dynamic type and the dynamic type is 
still pointing at a Sub object…

So the take away point is:

1. Dynamic type is all that matters on method call, I mean non-static methods.
2. Sub class static type var can NOT point to Super objects.
3. Super static type var CAN point to Sub objects.

In terms of Casting:

1. Casting Super var to Sub is NOT OK, where the dynamic type was Super
2. Casting to Super of Sub var is OK, but doesn’t gain us anything, while 
         the dynamic type was Sub object anyway.


Simple clarity, left side of assignment is static type, right side is dynamic type
(which contains the ‘new’ keyword).

No comments:

Just some daily notes ...

Powered By Blogger