• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instaceof operator

 
Sar Gops
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,

In K & B ,page no:286 i foung Foo[1] type is compared against Object,Foo,faces,Bar.and the result is true.can any one tell me what is the reasons.also Please tell me about the last three rows in that table.

Thanks in advance
 
Thirugnanam Saravanan
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For this question, the scenarion given in the book is :

interface Face { }
class Bar implements Face{ }
class Foo extends Bar{ }

The Thumb rule for working with instanceof Operator is as follows :

whenever a Class extends another Class or implements an interface, the following condition will return true :

1. sub-class instanceof super-Class - TRUE
2. implementing-class instanceof interface - TRUE

The vice versa is false in both 1 and 2 above.

Ok .. coming back to your question..

Foo[1] represents a Foo object which extends Bar (means it implicitly implements the interface Face also).

So Foo instanceof Foo,Bar,Face,Object will return TRUE.

Also Foo[] is an array. Any Array is an instanceof Object.

So Foo[] instanceof Object will return TRUE.
 
Jart Bo
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In other words, the instanceOf operator checks if the object passes the IS-A test.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic