overridding in inheritance
class Client {
public static void main(String[] args) {
String stringRef = new String("Java");// (1)
System.out.println("(2): " + stringRef.getClass());// (2)
System.out.println("(3): " + stringRef.length()); // (3)
Object objRef = stringRef;// (4)
//System.out.println("(5): " + objRef.length());// (5) Not OK.
System.out.println("(6): " + objRef.equals("Java"));// (6)
System.out.println("(7): " + objRef.getClass()); // (7)
stringRef = (String) objRef;// (8)
System.out.println("(9): " + stringRef.equals("C++"));// (9)
}
}
Output from the program:
(2): class java.lang.String
(3): 4
(6): true
(7): class java.lang.String
(9): false
Could any one tell me?
1.why can't we call the length() method on objRef reference at line mentioned 5 ,as it is pointing to the object of class String at runtime which defines the method length(),because for method to be called it is the object which is being actually reffered at runtime and not the reference type?
But if incase of calling the equals(), the method was called from String Class why it is unable to vall the length() method from subclass String
Is it ,as there is no overriding for length() method done?
Thanks in advance
[1329 byte] By [
dony908a] at [2007-11-27 8:15:40]

> Could any one tell me?
> 1.why can't we call the length() method on objRef
> reference at line mentioned 5
because the reference type is Object and class Object does not declare a method length.
> which defines
> the method length(),because for method to be called
> it is the object which is being actually reffered at
> runtime and not the reference type?
> But if incase of calling the equals(), the method was
> called from String Class
because class Object does declare method equals
the compiler allows calls depending on the compile-time reference type. calls that are allowed are then resolved at runtime depending on the object type.
> u mean that the compiler
> sceems
sceeming?
> because such a call is not allowed at compile
> time as during compile time the method called based
> on the reference object and as the reference object
> is of Object class which does not define the
> length() method and then this is resolved at runtime
> during where theobject actualluy reffered is taken
> for calling the appropiate method.
as far as i can follow that sentence it doesn't seem to be incorrect
> need some more clarification.
*sigh*
Object obj = new String();
obj.length(); //1
obj.equals(""); //2
it goes like this:
1:
compiler: type of obj is Object, "length" not defined, error, end of story
2:
compiler: type of obj is Object, "equals" defined, okay, create virtual function call
runtime: class of object pointed to by obj is String, which overrides equals, so String.equals is called
Object obj = new String();
((String) obj).length(); //1
This works, because you cast obj as a String, then calling length() on a String is perfectly legal.
Be careful with casts though, they can cause exceptions at runtime.
Does this help you a bit?