aspose file tools*
The moose likes Beginning Java and the fly likes Casting Problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Casting Problems" Watch "Casting Problems" New topic
Author

Casting Problems

Atrus Greyor
Ranch Hand

Joined: Mar 07, 2004
Posts: 110
For example I have class A, B, C and D all of which extend Z

Now I have ArrayList<Z> list = new ArrayList<Z>()

I've put several objects of type A, B, C and D in the list, randomly, and I don't know anything about the order.

How do I pull these items out with the correct type?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Atrus Greyor wrote:For example I have class A, B, C and D all of which extend Z

Now I have ArrayList<Z> list = new ArrayList<Z>()

I've put several objects of type A, B, C and D in the list, randomly, and I don't know anything about the order.

How do I pull these items out with the correct type?


There is no option to "pull these items out" by a type -- as all the items in the ArrayList is of type Z. You have to iterate through the list of Z's, and check for the type you want (using the instanceof operator).

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Atrus Greyor
Ranch Hand

Joined: Mar 07, 2004
Posts: 110
What if I know that the class is a subclass of Z, but I don't know the name of the class itself?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Atrus Greyor wrote:What if I know that the class is a subclass of Z, but I don't know the name of the class itself?


One option is to use reflection -- call the getClass() method to get the Class object, which you can then use to determine the type.

Henry
Atrus Greyor
Ranch Hand

Joined: Mar 07, 2004
Posts: 110
And then how would I go and cast that? I haven't ever used reflection, at least not knowingly.
Matthew Cox
Greenhorn

Joined: May 25, 2009
Posts: 29
You can't cast to it at Compile time if I am understanding your question correctly. When using Reflection, you are essentially giving up compile-time type checking and all the conveniences that come with it in order to gain the ability to "dynamically cast" as people seem to always describe it. Read up on Reflection and examine some coding examples. Reflection is tedious, run-time error prone (if not carefully coded), and if I remember correctly ... has a lot of overhead associated with it (not sure on this one). So be careful about how and when you decide to use it.

Someone in an earlier post mentioned a much simplier design ... it won't scale well but if you are only handling 4 or 5 classes and interfaces tops ... then creating a switch block of if block and using instanceof would be the best (meaning easiest) route. =D

GL
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Atrus Greyor wrote:And then how would I go and cast that? I haven't ever used reflection, at least not knowingly.


There is a method that you can use to cast objects... but of course, that doesn't answer your question, as the next question would be "how would you know what to assign it to after you cast it"?

You basically have to options...

1. You can use the Class object to determine what it is, and use that in a condition to cast and assign (as you would normally do).

2. You don't cast. You stay with reflection, and locate the method (or variables) that you want, and use reflection to access it.

Henry
Atrus Greyor
Ranch Hand

Joined: Mar 07, 2004
Posts: 110
Matthew Cox wrote:You can't cast to it at Compile time if I am understanding your question correctly. When using Reflection, you are essentially giving up compile-time type checking and all the conveniences that come with it in order to gain the ability to "dynamically cast" as people seem to always describe it. Read up on Reflection and examine some coding examples. Reflection is tedious, run-time error prone (if not carefully coded), and if I remember correctly ... has a lot of overhead associated with it (not sure on this one). So be careful about how and when you decide to use it.

Someone in an earlier post mentioned a much simplier design ... it won't scale well but if you are only handling 4 or 5 classes and interfaces tops ... then creating a switch block of if block and using instanceof would be the best (meaning easiest) route. =D

GL


It's hard to do an if tree when the only information you have about the class is that it is a child of Z. It could be a class named A, B or something completely random.

However, I was able to solve it through reflection, thanks a lot Henry.
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
Atrus Greyor wrote:For example I have class A, B, C and D all of which extend Z
How do I pull these items out with the correct type?

What do you need to do with these objects? In typical OO application design, as you process a collection of object you would be running a method introduced in the common ancestor (Z). Because of polymorphism, you wouldn't need to cast them.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting Problems