Forums Register Login

Objectoreinted concepts

+Pie Number of slices to send: Send
class Client {
public static void main(String args[]) {
String stringRef = new String("java");
System.out.println("2):" + stringRef.getClass());
System.out.println("(3):" + stringRef.length());
Object obref = stringRef;
System.out.println("(6):" + obref.equals("Java"));
System.out.println("(7):" + obref.getClass());
stringRef = (String)obref;
System.out.println("(9):" + stringRef.equals("C++"));
System.out.println("(5):" + obref.length());
}
}

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.

+Pie Number of slices to send: Send
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 ?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
hi.
Mosts probally i think when u type cast obj ref to string still y are using method which is not in Object directly..
Thats why u are getting erors
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
I think i am wrong. Val stated that the obref is not an object.
It just an reference
+Pie Number of slices to send: Send
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.
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 577 times.
Similar Threads
where is the Object o1 come from?
Marcus Green exam 2 Q.50
why this happens?
Marcus Exam#2 Q50
assigning up the hierarchy
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 08:36:37.