• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

obj conversion

 
kavita s. kumar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
since floater ia an object of wrapper class..is it not we are converting an object to an object?
 
Adrian Muscalu
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
:-)
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic