Consider the statement
Base a = new Agg();
Eventhough you are instantiating an object of class Agg, due to reference assignment to type 'a', compiler checks to see if the method invocation( or variable reference ) is valid with regard to class Base. By assigning the new 'Agg' object to type 'a', you are actually pointing to the 'Base' class instance contained in the derived object of type 'Agg'. This means if you need to access any of the attributes/methods of the derived class, you will have to re-cast the object to its original type, ie., 'Agg'.
Polymorphism is a runtime behaviour which can be observed if the
actual type of object being pointed to overrides any method in the base class, in which case the newer version of the method gets executed.
Sivaram, you said -
Base v = new Child();
v.aMethod(); would execute the child's over loaded method. And v.aVeriable would call the Base aVariable.
( Please see the text in bold. )
I think you meant
overridden method and not
overloaded method. Please make sure you understand the difference between the two. Overloaded methods are actually different methods that only share the common name. Unlike overridden methods, overloaded methods do not exhibit "true" polymorphism. You can override a method only once, whereas you can overload a method any number of times with different signatures.
Ajith