this code doesn't compile. I understand that length() method is not avialable in the Object class so it gives the error. But i have type casted obref into stringRef so why does it gives the error.
Don't know if i am right but i'll try. The reason why there is no length i think is because you just cast the reference that points to an Object not an actual object so therefore Object gives an error because it doesn't provide a length method. Am i right ?
Val, you are right. When you convert your object hier up, if you want to use methods of the subclass they have to be in the class you converted it too also. If they are not, then you will get a compile error. If they are, then you can call that method and you will get the method of the subclass, not superclass (unless the method is static). That is one of the reasons why interfaces and abstract classes can be so important. They make sure that the method is in the class, so you can use late-binding and polymorphism. The I/O package is a great example of these techniques. Bill
hi! It is given in 6 chapter of khalid mughal that this code gives the error beacause length() is not in the Object class. I got the above code from 6.2 illustrated example. But i don't think in that way. May be the code is giving error for some other reason like Val stated.
At compile time, whatever may be the object an reference is pointing to, what the compiler checks is this: Whether the class of the reference has the method or whether it inherits the method from its superclasses. If it does not fir into the above two criteria, then nobody can rescue u.
Post by:autobot
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
a bit of art, as a gift, the permaculture playing cards