• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using instanceof operator

 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interface Face { }

class Bar implements Face { }

class Foo extends Bar { }

Results when using the instanceof operator :

Foo [] instanceof (Foo , Bar , Face ) --- False

Foo[1] instanceof (Foo, Bar , Face , Object ) --- True

Can some one please explain the above ?
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,



It is because, any Foo object is an instance of Foo.
Foo is an instance of Bar, because Foo extends Bar.
Foo is an instance of Face because Class Bar implements Face and Bar is a direct super-class of Foo, so Foo is an instance of Face as well.
Any class in Java is an instance of Object.

Hope this helps,
 
John Mercier
Greenhorn
Posts: 26
Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, Foo[] is an Array not a Foo.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jose,

Additionally, when you ask some question, please separate it from comments. Else hard time to understand your question.

Please check this out



Foo[] is an array and an array is only an instance of Object.
Foo[1] is talking about a specific object.

Hope this will help you clear your doubt.

Happy Preparation,
 
Anup Om
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Foo [] instanceof (Foo , Bar , Face ) --- False


This will cause compilation error(if written as Java code) as Foo or Bar or Face isn't supertype of Foo[]. (i.e they don't fall under same hierarchy).
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for responding.
 
sumit kothalikar
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It is because we have made Foo[] as an array object
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic