Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Objectoreinted concepts

 
shabbir zakir
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Val Dra
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
prashant joshi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
shabbir zakir
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
shabbir zakir
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think i am wrong. Val stated that the obref is not an object.
It just an reference
 
natarajan meghanathan
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Reply
  • Bookmark Topic Watch Topic
  • New Topic