File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt About Object Reference Casting Rule 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 "Doubt About Object Reference Casting Rule" Watch "Doubt About Object Reference Casting Rule" New topic
Author

Doubt About Object Reference Casting Rule

Sujit Kurtadikar
Ranch Hand

Joined: Dec 05, 2000
Posts: 68
friends,
RHE book states following compile-time rule for Object ref casting,
Oldtype: non-final class & Newtype : Array -> Newtype must be Object

Oldtype: Array & Newtype: non-final class -> Oldtype must be Object

Object --> Fruit --> Citrus (implements Squeezable) -->Lemon
-->Tangelo
-->Grapefruit
Tangelo tang = new Tangelo();
Object[] objAry;
objAry = tang; // as per rule this should complile, but won't
However, Object ob = tang; //This will compile
Can somebody clarify this rule.
sujit

Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Sujit,
I don't see any problem with the reasoning. You are complaining about trying to convert an non-final class to an array. That is not possible in Java. Your third line gives you the answer:
non-final class to Array -> Newtype must be Object.
You are trying to read it with the last word being Array instead of Object!
In Java an array is an object therefore we can always do the following:
int[] intArray = new int[25];
Object ob = intArray
or
MyClass c = new MyClass();
Object ob = c;
We can not however, do the following:
int[] intArray = new int[25];
Object[] obArr = intArray;
or
MyClass c = new MyClass();
Object[] obArr = c;
In your case you could have performed the following:

Regards,
Manfred.
Howard Hu
Greenhorn

Joined: Jan 10, 2001
Posts: 17
Sujit,
I have the same doubt.
RH rule for casting reference:
Oldtype: non-final class & Newtype : Array -> Newtype must be Object
When Newtype is first said as Array, how come the rule is that Newtype must be Object? We are talking about array as new type, not Object! Then we try Object[] as new type, wrong too.
You may argue that an array is an object; but an object of Object type is not always an array. So I think the book should make this more clear.

Howard
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
What RHE is saying is that if you are trying to convert a non-final class to an array, the best you can do is convert it to an Object, the superclass of an array. Basically that means you can't convert a non-final class to an Array.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi all,
I don't own the RHE book and am not sure what the rules are referring to; however, you can cast/assign arrays if the array type is legally assignable.

The above assignments and cast compile and run ok.
If you comment out '1', the code compiles ok but produces a runtime error as Fruit cannot be legally assigned to an Apple type.

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Howard Hu
Greenhorn

Joined: Jan 10, 2001
Posts: 17
Jane,
Thanks. The issue here is not about whether you can cast an array to an array. It is about one rule in the book talking about casting from a non-final class to an array. I think the discussion so far has made that issue very clear.
--Howard
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Just to add to the discussion, we've had a handful of readers write in and report our explanation here as an error. Possibly the rule is too terse at first sight, but it is correct. Thanks to Bill and Manfred for explaining our intent so clearly!

------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Sujit Kurtadikar
Ranch Hand

Joined: Dec 05, 2000
Posts: 68
Thanks to Manfred and Bill for giving clarification about my doubt.
special thanks for Michael Ernest, really I didn't expect the reply from author of book. Thanks Michael.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt About Object Reference Casting Rule