aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes obj conversion 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 "obj conversion" Watch "obj conversion" New topic
Author

obj conversion

kavita s. kumar
Greenhorn

Joined: Oct 26, 2001
Posts: 16
Consider the following code:
1. Object ob = new Object();
2. String stringarr[] = new String[50];
3. Float floater = new Float(3.14f);
4.
5. ob = stringarr;
6. ob = stringarr[5];
7. floater = ob;
8. ob = floater;
it says Changing an Object to a Float (in line 7 )is going "down" the inheritance hierarchy tree, so an explicit cast is required.
please explain
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Originally posted by kavita s. kumar:
Consider the following code:
1. Object ob = new Object();
2. String stringarr[] = new String[50];
3. Float floater = new Float(3.14f);
4.
5. ob = stringarr;
6. ob = stringarr[5];
7. floater = ob;
8. ob = floater;
it says Changing an Object to a Float (in line 7 )is going "down" the inheritance hierarchy tree, so an explicit cast is required.
please explain

Object is supreme. So when you use Float as destination and Object as source ... of course there will be error
Its downcasting
Now to avoid this cast...(Float) ob...
Ragu
kavita s. kumar
Greenhorn

Joined: Oct 26, 2001
Posts: 16
since floater ia an object of wrapper class..is it not we are converting an object to an object?
Adrian Muscalu
Ranch Hand

Joined: May 08, 2000
Posts: 73
:-)
Both a cat and a dog are animals but still different...
If you say a cat is an animal (Animal an = new Cat()) it's ok but but saying that an animal is a cat (Cat cat = new Animal()) is tricky; it may be, it may be not, unless you know for a fact that the animal you are talking about is indeed a cat.

SCJP2, SCWCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: obj conversion