• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using instanceof operator

 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Ranch Hand
Posts: 49
1
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for responding.
 
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It is because we have made Foo[] as an array object
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic