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?
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
publicclass Test
{
publicstaticvoid 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();
}
publicstatic Animal checkAnimalType( Animal animal )
{
if( animal instanceof Dog )
{
return (Dog)animal;
}
elseif( animal instanceof Cat )
{
return (Cat)animal;
}
return animal;
}
}
interface Animal
{
void talk();
}
class Dog implements Animal
{
publicvoid talk()
{
System.out.println("Bow wow Bow wow...Do you need any more proof that am a DOG?");
}
}
class Cat implements Animal
{
publicvoid talk()
{
System.out.println("meow meow...Do you need any more proof that am a CAT?");
}
}
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.
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.
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.)