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 7007029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:33:15+00:00 2026-05-27T21:33:15+00:00

I was wondering how I could handle polymorphism in a for loop without using

  • 0

I was wondering how I could handle polymorphism in a for loop without using of “instanceof” like in C#.
Here is what I have :

An abstract class A :

public abstract class A
{
    public abstract Map<Long, ? extends A> getNodes();
    public abstract void setNodes(Map<Long, ? extends A> n);
}

B and C classes which extends A and implements abstract methods of A :

public class B
{
    private Map<Long, C> childNodesC;

    @Override
    public Map<Long, ? extends A> getNodes()
    {
        return childNodesC;
    }

    @Override
    public void setNodes(Map<Long, ? extends A> n)
    {
        for(C child : n)
            childNodesC.put(child.getId(), child);
    }
}

Is a for-loop like this one in setNodes() method, possible in Java ? I mean, is there a way for Java to understand that, in my loop, I want to iterate only over all C objects in a list of A objects (which here could be either B or C objects).

Or maybe I don’t use polymorphism in the right way or something like that…or maybe this is a true case of “instanceof” operator usage 🙂

(Yeah you’re right, I don’t like “instanceof” operator, it makes me think that I didn’t do the things correctly so I need to fix them with casts and instanceof. It looks “dirty” to me, but maybe I’m wrong and it is very common to use it !)

Thanks !

  • 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-05-27T21:33:16+00:00Added an answer on May 27, 2026 at 9:33 pm

    There are sometimes cases where you need to know of which subclass an instance is. We can’t really determine if your case is one of those. But if you need to implement this you should consider using the Visitor Pattern and only implement the method of your wanted subclass. This way you avoid the ‘dirty’ instanceof checks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering how could I reload any website using javascript and set it
I was wondering if anyone could point to an Open Source date utility class
I was wondering if you could help me out.. I have two classes, one
I'm wondering what is the culturally-acceptable way to handle this code situation. I have
I'm using TBB and was wondering how I could use IOCP with its task
I just wondering if one plugin could handle all of this. Content Filtering, Sorting
I have an interface and a class defined as below public interface IShape {
Just wondering how how people handle Authorization when using MVVM and/or View Models. If
I was wondering what could be the point in trying to delete committed changelists,
I've got an issue that was wondering if could be solved in a particular

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.