Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8956151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:43:28+00:00 2026-06-15T14:43:28+00:00

How does instanceof work exactly? When I have a hierarchy of objects that extend

  • 0

How does instanceof work exactly? When I have a hierarchy of objects that extend and implement eachother, does being instance of something work through these both lines?

For example I want to know if my object is instance of Listor ArrayList or Collection?

I examined this tree, http://docs.oracle.com/javase/6/docs/api/java/util/package-tree.html

And they seem to fall all under Object ofcourse, but what I need, I think is AbstractCollection or even normal Collection , because that seems to be the highest in hierarchy.

Will I be fine, when I check an object against only Collection to cover all those 3 classes?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:43:30+00:00Added an answer on June 15, 2026 at 2:43 pm

    Will I be fine, when I check an object against only Collection to cover all those 3 classes?

    Yes, instanceof Collection will return true for all implementation (direct or indirect) of the Collection interface.

    In the rare case that you do not want this, you’d have to use reflection. For example, Class#getDeclaredClasses will give you a list of all classes and interfaces that are directly extended/implemented by the class.

    Once you know that something is a Collection, you can cast it to get access to its methods (like iterator):

      if (myObject instanceof Collection){
          Collection<?> c = (Collection<?>) myObject;
          for (Object o: c){
             // do something with every element in the collection
          }
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Java code that exactly does this: // Create a Service instance Dispatch<SOAPMessage>
Why does my Instantiate function not create a 'Blank' instance of That? I have
If you have an object instance A that references other objects (for example instances
Does anyone know how exactly RSLs work with AIR? I have a terminal server
The Question: When you UnicastRemoteObject.exportObject(instance) . Does that instance now become publicly available to
I have code that all over the place(probably 20-30 instances) does this: <widget>.setVisible((condition ==
Does the entrySet() function that is called from a treemap instance return a TreeSet
For instance, I want to have a html form that looks like this: <table>
I'm trying to extend ScriptManager to simplify dealing with resources that have multiple resource
what i have here works exactly how i want it. My question is that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.