wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Object types in an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object types in an array" Watch "Object types in an array" New topic
Author

Object types in an array

Mike Bursill
Greenhorn

Joined: Oct 15, 2003
Posts: 1
The short version of my question (Using "How my Dog learned Polymorphism article" as the example):

If I had the d object (the Dog class inherited from the Animal class) with the added method Bark() stored into an array of Animals[] how could I ever cycle through all the objects in the array, figure out that one of them is Dog and access the method Bark()?

The longer more detailed version of my question:

Just for fun, I'm developing a few C# programs (the problem isnt C# specific, just OO specific). One of these programs is to skin C# winforms. I have most if it completed, including the reading of the files which make the skin from a compressed zip, and also the parsing of an XML file defining all the elements and there locations. However, I have run into one small OO glitch.

I have 3 class's:

class SkinFiles
{
public String Type = "";
public String ID = "";
public String FileName = "";
}

class ImageFile:SkinFiles
{
public Color ColorKey;
}

class SoundFile:SkinFiles
{
//not implemented yet
}


As I parse the XML file (defining the Skin), it tells me the Type/ID and Name of a file I am going to need form the ZIP (which holds all the skins stuff, pic's sounds' etc.). I create a new instance of the ImageFile or SoundFile object to hold this info depending on what the file type is (Also defined by the XML). I then store the object into a collection.

I am using a collection, so that I can reference the objects via a string (the name of the file). I do this so when I cycle through all the files of the ZIP, I can tell instantly the ID to be assigned to the incoming file, and possibly a ColorKey or other file specific info to that type of file.

Since the collection stores the class as type "Object", when I need to reference it, I must type cast it back to whatever type it is.

My problem is, how do I know what that type is? I can type caste it back to SkinFiles and get access to those public instance variables, but I loose the ability to read the ColorKey variable if the saved object so happened to be a ImageFile object.

Thanks!

-Mike.
Jacquie Barker
author
Ranch Hand

Joined: Dec 20, 2000
Posts: 201
Mike, there are several ways to "interrogate" objects in a collection to find out their types, as follows:
if (x.getClass().getName().equals("Dog")) {
Dog d = (Dog) x;
d.bark();
}
or
if (x instanceof Dog) { ... }
Hope this helps!

Originally posted by Mike Bursill:
The short version of my question (Using "How my Dog learned Polymorphism article" as the example):

If I had the d object (the Dog class inherited from the Animal class) with the added method Bark() stored into an array of Animals[] how could I ever cycle through all the objects in the array, figure out that one of them is Dog and access the method Bark()?

The longer more detailed version of my question:

Just for fun, I'm developing a few C# programs (the problem isnt C# specific, just OO specific). One of these programs is to skin C# winforms. I have most if it completed, including the reading of the files which make the skin from a compressed zip, and also the parsing of an XML file defining all the elements and there locations. However, I have run into one small OO glitch.

I have 3 class's:

class SkinFiles
{
public String Type = "";
public String ID = "";
public String FileName = "";
}

class ImageFile:SkinFiles
{
public Color ColorKey;
}

class SoundFile:SkinFiles
{
//not implemented yet
}


As I parse the XML file (defining the Skin), it tells me the Type/ID and Name of a file I am going to need form the ZIP (which holds all the skins stuff, pic's sounds' etc.). I create a new instance of the ImageFile or SoundFile object to hold this info depending on what the file type is (Also defined by the XML). I then store the object into a collection.

I am using a collection, so that I can reference the objects via a string (the name of the file). I do this so when I cycle through all the files of the ZIP, I can tell instantly the ID to be assigned to the incoming file, and possibly a ColorKey or other file specific info to that type of file.

Since the collection stores the class as type "Object", when I need to reference it, I must type cast it back to whatever type it is.

My problem is, how do I know what that type is? I can type caste it back to SkinFiles and get access to those public instance variables, but I loose the ability to read the ColorKey variable if the saved object so happened to be a ImageFile object.

Thanks!

-Mike.

[ October 15, 2003: Message edited by: Jacquie Barker ]

Author of Beginning Java Objects, Beginning C# Objects, and Taming the Technology Tidal Wave
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
If I had the d object (the Dog class inherited from the Animal class) with the added method Bark() stored into an array of Animals[] how could I ever cycle through all the objects in the array, figure out that one of them is Dog and access the method Bark()?
Let me point out that the point of using Polymorphism in many situations is so that you would never do such a thing. In more polymorphic conditions, you'd perhaps have a collection of animals that can all speak in one way or another - dogs bark, birds chirp, cows moo, etc. Then, you'd iterate over the collection and just tell each animal to speak - you would't care what type the animal were. The dogs would bark, the birds would chirp, the cows would moo, and all would be right with the world. (OK, so maybe that last part is a bit much to ask of polymorphism.)


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object types in an array
 
Similar Threads
Example for Certification book doesnt work
Learning about objects
Java head: object references variables
Why can only "real" dog bark?
Creating Objects