The Artima Developer Community
Sponsored Link

Java Answers Forum
Dynamic type casting

5 replies on 1 page. Most recent reply: Mar 7, 2002 10:24 PM by Anil S.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Chad Daniels

Posts: 2
Nickname: chad
Registered: Mar, 2002

Dynamic type casting Posted: Mar 7, 2002 5:28 PM
Reply to this message Reply
Advertisement
I need to know how to pass the type of an object into a method and then use the passed type to cast a generic Object so that I can use specific methods.

For example, lets say I'm creating a program for the local dog pound:
I have an ArrayList with Dog objects.
I loop through the ArrayList and retrieve one Dog at a time (retrieved as type Object using Iterator).
Each time I retrieve a Dog, I want to see if it's the Dog I need, so I pass it to a method that will tell me if it's the right Dog.
I wish, however, to have this be a static method in another class so that I can use it to find Cats @ the 'cat pound' as well, so I want to make the method generic.
Is there a way to pass in the Object type (Dog or Cat) to the method along with the generic Object from the ArrayList, so that I can do a type cast and compare the selected Dog against the desired Dog?

Thanks for any help.


Anil S.

Posts: 13
Nickname: anil
Registered: Feb, 2002

Re: Dynamic type casting Posted: Mar 7, 2002 8:52 PM
Reply to this message Reply
Here is a quick piece of code if I understand your
question, it's a proof of concept, you can be more
creative in your code, guess you get da point, lemme
know if this is what you wanted...

Artima has great resources on Interfaces.

Interfaces Involved:
Animal.java

Classes Involved:
Dog.java
Cat.java
Test.java

public class Test
{
   public static void main( String[] args )
   {
      Animal myDog = new Dog();
      Animal myCat = new Cat();
      
      java.util.List animalStore = new java.util.ArrayList();
      
      animalStore.add( myDog );
      animalStore.add( myCat );
      
      Test t = new Test();
      
      Animal dontKnowWheterCatOrDog = (Animal)( animalStore.get(0) );
      
      // uncomment the below code to test another animal
      // Animal dontKnowWheterCatOrDog = (Animal)(animalStore.get(1));
      //
      // Force the animal to blurt out something
      // then we'll know for sure it's kind breed blah blah...be kind to animals!!!
      
      ( t.checkAnimalType( dontKnowWheterCatOrDog ) ).talk();
   }
 
   public static Animal checkAnimalType( Animal animal )
   {
      if( animal instanceof Dog )
      {
         return (Dog)animal;
      }
      else if( animal instanceof Cat )	
      {
         return (Cat)animal;
      }
      
      return animal;
   }
}
 
interface Animal
{
	void talk();
}
 
class Dog implements Animal
{
	public void talk()
	{
	   System.out.println("Bow wow Bow wow...Do you need any more proof that am a DOG?");
	}
}
 
class Cat implements Animal
{
	public void talk()
	{
		System.out.println("meow meow...Do you need any more proof that am a CAT?");
	}
}
 

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Dynamic type casting Posted: Mar 7, 2002 9:58 PM
Reply to this message Reply
Your method checkAnimalType() isn't really doing anything, since it is returning Animal no matter what. The reason your code works fine is that both Cat and Dog implement[code] the interface Animal and that is what is being used. The only reason you'd want to differentiate between different subclasses at is to call methods that were not part of a common interface (which means you could simply call [code]dontKnowWheterCatOrDog.talk() without calling checkAnimalType()). Of course, if you can stick to the interface for all of your special types of Animals, you will have a cleaner and easier-to-work-with design.

If you really wanted to return specialized objects, you'd have to have different methods, something like gimmeADog(Animal a) and gimmeACat(Animal a). Of course, they would return null if an Animal of some other type were passed in.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Dynamic type casting Posted: Mar 7, 2002 10:00 PM
Reply to this message Reply
Oops! I thought was one of the available formatting tags! Here it is with [b]bold[/b] instead:

> Your method [b]checkAnimalType()[/b] isn't
> really doing anything, since it is returning
> [b]Animal[/b] no matter what. The reason your code
> works fine is that both Cat and Dog
> [b]implement[/b] the interface Animal and that
> is what is being used. The only reason you'd want
> to differentiate between different subclasses at is
> to call methods that were not part of a common
> interface (which means you could simply call
> [b]dontKnowWheterCatOrDog.talk()[/b] without
> calling [b]checkAnimalType()[/b]). Of course,
> if you can stick to the interface for all of your
> special types of Animals, you will have a cleaner and
> easier-to-work-with design.
>
> If you [i]really[/i] wanted to return specialized
> objects, you'd have to have different methods,
> something like [b]gimmeADog(Animal a)[/b] and
> [b]gimmeACat(Animal a)[/b]. Of course, they
> would return [b]null[/b] if an Animal of some
> other type were passed in.

Bill Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

Re: Dynamic type casting Posted: Mar 7, 2002 10:19 PM
Reply to this message Reply
Yes, Matt actually once requested that feature over coffee, then he must have subconsciously convinced himself it exists. It's a good idea, which I'd like to add someday. In the meantime, that preview button can avoid such problems (just so long as the problem isn't a funny character that looks OK in the preview but comes out a question mark in the actual post.)

Anil S.

Posts: 13
Nickname: anil
Registered: Feb, 2002

Re: Dynamic type casting Posted: Mar 7, 2002 10:24 PM
Reply to this message Reply
Matt, you're quite right ther', hopefully it's
a good place to start...

Flat View: This topic has 5 replies on 1 page
Topic: Runtime.process.exec() Previous Topic   Next Topic Topic: javadocs for com.ibm.websphere.csi.*

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use