aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Objectoreinted concepts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Objectoreinted concepts" Watch "Objectoreinted concepts" New topic
Author

Objectoreinted concepts

shabbir zakir
Ranch Hand

Joined: Nov 12, 2000
Posts: 183
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

Joined: Jan 26, 2001
Posts: 439
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 SCJP <BR>going for SCJD
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
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

Joined: Feb 02, 2001
Posts: 2
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

Joined: Nov 12, 2000
Posts: 183
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

Joined: Nov 12, 2000
Posts: 183
I think i am wrong. Val stated that the obref is not an object.
It just an reference
natarajan meghanathan
Ranch Hand

Joined: Feb 01, 2001
Posts: 130
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.

Sun Certified Programmer for Java 2 Platform
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Objectoreinted concepts