The method in the derived class doesn't override the one in the base one because it hasn't got the same signature. Replace char with int and you'll obtain polymorphic behaviour. "I love to say that ;-)"
method is invoked based on the reference and not based on Class type . but if this is the case then the output should be 'X' kindly can one explain
Think Big . So shall you become big.
Joined: Apr 14, 2001
Monzy, the methods are invoked on basis of the actual type and variables are referred on basis of the reference type. [This message has been edited by Angela Narain (edited September 29, 2001).]
Joined: Jul 03, 2001
First think like the compiler: look for the method declaration to invoke in the class denoted by the type of the variable, or its superclasses. If there are none matching declaration the compiler will give an error. In this case in class B there is a matching method, so the compiler generates the class file and go for a deserved rest. Now think as the JVM executing the code: Is the method to invoke (the one determined by the resting compiler) private or static? if yes invoke it. If not it looks for any overriding method in the class of the actual object hold in the variable (A) and invoke it. But here there is no such method, so the base one is called.