This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceof doubt... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instanceof doubt..." Watch "instanceof doubt..." New topic
Author

instanceof doubt...

Siri Naray
Ranch Hand

Joined: May 19, 2006
Posts: 105
Hai ranchers
could any one explain me the use of instanceof?

If B inherits A
which one is true
1. A instanceof B
2. B instanceof A
3. Or both work fine?

Thank you


If you worry you cannot work... If you work you need not worry
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

If b is the subclass then b instanceof A is true


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by Sirishaaaaa Ghatty:
Hai ranchers
could any one explain me the use of instanceof?

If B inherits A
which one is true
1. A instanceof B
2. B instanceof A
3. Or both work fine?

Thank you


Actually neither is syntactically correct.

The left-hand operand of instanceof must be a reference name, and the right-hand side must be a reference type.

Also, if the class type of the reference name is not a subclass nor a superclass of the class type on the right-hand side, then a compile-time error occurs.

So, for example,


will print true.

But



will cause a compile-time error.

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.20.2
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Consider an example: Cat extends Animal.

An Animal is always an Animal.
A Cat is always a Cat.
Furthermore, a Cat is always an Animal.

But is an Animal always a Cat? Sometimes, but not always. This is where instanceof is useful.

(Note that the left side must be a reference, and the right side must be a type. You cannot compare two types using instanceof, as your example implies.)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Siri Naray
Ranch Hand

Joined: May 19, 2006
Posts: 105
Hi John,
Can you please explain me this

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()
Cannot find symbol : method playDead() at class Animal animal.playDead();

Does it mean at compile time "animal instanceof Dog" is true?
Only when "if" statement is true does the compiler go to next statement.
Can you please explain.
Thank You
Siri Naray
Ranch Hand

Joined: May 19, 2006
Posts: 105
Thank you i got the answer. 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
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
The problem is that the reference is of type Animal, and not of type Dog, even though the runtime type of the object is Dog.

You cannot call methods defined in the subclass (and not defined in the superclass) using a superclass reference.
rohitt kumarr
Greenhorn

Joined: Jul 27, 2006
Posts: 5
You need to cast it to the type Dog if you want to invoke the playdead method
 
jQuery in Action, 2nd edition
 
subject: instanceof doubt...
 
Similar Threads
KAM India Edition Question 6.22
instanceof Comparison
Instance Of
instanceof operator
how discover subclasses of a class?