Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instanceof doubt...

 
Siri Naray
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If b is the subclass then b instanceof A is true
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Siri Naray
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to cast it to the type Dog if you want to invoke the playdead method
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic