Hi Vivek,
This is an essential part of
Java and its object oriented nature
(
polymorphism).
When you declare a reference variable of class A and create or
instantiate it with an object of subclass B, then two things
happen when you call a method m1() with that reference:
1.) at compilation time the compiler looks for m1 amongst the
(overloaded) methods in the class of the reference type A.
2.) at runtime the JVM looks in the class of the object type B
for any overridden version of the method or just takes the
inherited method.
Suppose that the compiler finds no method in step 1 then it
gives an error. The reason for this is that step 2 is a runtime
check and the compiler doesn't necessarily have to know what
the object will be at runtime due to polymorphism.
Cheers,
Gian Franco
[ June 04, 2004: Message edited by: Gian Franco Casula ]