Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Casting Object Type in Collection

Posted on 2004-11-08
12
Medium Priority
?
200 Views
Last Modified: 2010-03-31
I need to cast a collection of objects to a subclassed object, but continue to receive an ArrayStoreException.  

For Example, the objects in the collection are all of a Dog type.  I'd like to cast the complete collection to a subclass I created called Dalmation that extends Dog...

//example code
List c = new java.util.ArrayList();
//...add collection of 'Dog' objects to c -> c.addAll(...);
c.toArray (new Dalmation[0]);

I thought this would work, however it doesn't.
What's the proper way to cast these objects in the collection?
0
Comment
Question by:sapientconceptions
  • 6
  • 4
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12529613
Dalmation[] d = (Dalmation[]) c.toArray (new Dalmation[0]);


0
 
LVL 92

Expert Comment

by:objects
ID: 12529616
thats assuming they are all Dalmations in your collection, if they are actually Dog's use:

Dog[] d = (Dog[]) c.toArray (new Dog[0]);
0
 

Author Comment

by:sapientconceptions
ID: 12529646
They are actually Dog's, however I'm trying to cast to my new object type, which extends dog.  So (Dalmation[])c.toArray(new Dalmation[0]) *should* work?  

Why exactly would I get an ArrayStoreException?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 92

Expert Comment

by:objects
ID: 12529661
because it tries to store an object of type Dog in an array of Dalmatioons
0
 
LVL 92

Expert Comment

by:objects
ID: 12529662
> They are actually Dog's, however I'm trying to cast to my new object type, which extends dog.  
> So (Dalmation[])c.toArray(new Dalmation[0]) *should* work?  

No, because a Dog is not a Dalmation so u can't store a Dog in an array of Dalmations.

0
 

Author Comment

by:sapientconceptions
ID: 12529794
Ok, I understand that.  Thanks for the explanation.

So, how can I 'wrap' the dog object in the Dalmation object?  I need to extend the Dog class somehow...
0
 

Author Comment

by:sapientconceptions
ID: 12529805
i believe it's called downcasting right?
0
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 12529816
You need to get an array of dogs, and cast individual elements to Dalmation (if they are Dalmataions)

eg.

Dog[] d = (Dog[]) c.toArray (new Dog[0]);
for (int i=0; i<d.length; i++)
{
   if (d[i] instanceof Dalmation)
   {
     Dalmation dal = (Dalmation) d[i];
     // do whatever with the dalmation
   }
}
0
 

Author Comment

by:sapientconceptions
ID: 12530078
Thanks for your help Objects.

Basically none of the elements will be of type Dalmation.  So in order for me to have a Dalmation object, I have to create a new instance of Dalmation and pass all the instance variables back through?  The problem I'm encountering is the super class may have a ton of variables...I was hoping there was some way to avoid having to do something like this:

//sample Dog class
public class Dog {
  private hasSpots = false;
  private isBigDog = false;

 public Dog(boolean hasSpots, boolean isBigDog) {
  this.hasSpots = hasSpots;
  this.isBigDog = isBigDog;
 }

 public boolean isBigDog() {
   return false;
 }

 public boolean hasSpots() {
   return false;
 }
}

//sample Dalmation class
public class Dalmation extends Dog {
  public Dalmation(boolean hasSpots, boolean isBigDog) {
   super (hasSpots, isBigDog);
 }

//additional dalmation methods...
}

0
 
LVL 92

Expert Comment

by:objects
ID: 12530099
> o in order for me to have a Dalmation object, I have to create a new instance of Dalmation and pass all the instance variables back through?

thats correct.
A subclass ctor must call one of its parent classes ctor's.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question