File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confused with instanceof, please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confused with instanceof, please help" Watch "Confused with instanceof, please help" New topic
Author

Confused with instanceof, please help

Siri Naray
Ranch Hand

Joined: May 19, 2006
Posts: 105
class Animal
{
void makeNoise(){System.out.println("generic noise");}
}
class Dog extends Animal
{
void makeNoise() {System.out.println("bark");}
void playDead() {System.out.println( "roll over");}
}
class CastTest2
{
public static void main(String [] args)
{
Animal [] a = {new Animal(), new Dog(), new Animal()};
for(Animal animal : a)
{
animal.makeNoise();
if(animal instanceof Dog)
animal.playDead();
}
}
}

When i compile this code compiler gives an error at animal.playDead()

This is probably because at compile time "animal instanceof Dog" is true

But what about runtime?

When i modified the code i posted above

if(animal instanceof Dog)
System.out.println(" animal is an instanceof Dog" );

output
-------
generic noise
bark
animal is an instanceof Dog
generic noise

But when "animal instanceof Dog" is true output should be

generic noise
animal is an instanceof Dog
bark
animal is an instanceof Dog
generic noise
animal is an instanceof Dog

I am confused.. please help
Thank you
--------------------


If you worry you cannot work... If you work you need not worry
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
An Animal has no playDead() method. Your reference variable animal is of type Animal so you cannot do animal.playDead().

Now, if at runtime, you know animal is going to be a Dog (using instanceof), you can call playDead() on this object, but only if you promise the compiler that this Animal is really a Dog. So what you have to do is cast animal to a Dog like this: (Dog)animal.
Then you can call playdead() like this: ((Dog)animal).playDead().

So your code should be:

[ July 27, 2006: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
By using instanceof before the casting of the animal to a Dog you have avoided a possible ClassCastException. So instead of having a nasty RuntimeException appearing suddenly out of nowhere, you just don't call playDead() when it is inapproriate to do so - like on a ChargingHeffalump or a ManEatingTigger (or worse, a Snark which turns out to be a BooJum).
[ July 27, 2006: Message edited by: Barry Gaunt ]
Siri Naray
Ranch Hand

Joined: May 19, 2006
Posts: 105
Thank You, its very clear now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confused with instanceof, please help