• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt About Object Reference Casting Rule

 
Sujit Kurtadikar
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1492
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1070
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Howard Hu
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7292
Netbeans IDE VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sujit Kurtadikar
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic