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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:49:59+00:00 2026-05-15T22:49:59+00:00

Attempting to generics-ify some legacy code, I’m stuck. I have a ParentObject which wraps

  • 0

Attempting to generics-ify some legacy code, I’m stuck. I have a ParentObject which wraps a ChildObject to provide group-type operations on the ChildObject. Most usefully, it allows iteration over the collection of child objects.
When I try to add some generics to the mix, I can’t work out how to make it play friendly with the iterator method without either a “naming clash” error, or in the example below, a “The return type is incompatible with Iterable.iterator()” error.
Any suggestions? (Bonus question – is there a better way to write the avoid thegetChildObjectByIndex() method to avoid type erasure complier warning other than suppressing the warnings?)
Thanks a lot in advance for any help

public class ParentObject implements Iterable<ChildObject> {  
    protected List<? super ChildObject> theChildObjects;  

    @SuppressWarnings("unchecked")  
    public <T extends ChildObject> T getChildObjectByIndex(int idx) {  
        return (T)theChildObjects.get(idx);  
    }  

    public Iterator<? super ChildObject> iterator() {  
        return java.util.Collections.unmodifiableCollection(this.theChildObjects).iterator();  
    }  

}
  • 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-15T22:50:00+00:00Added an answer on May 15, 2026 at 10:50 pm

    If ParentObject only contains one subtype of ChildObject, you could parametrize ParentObject on that type:

    public class ParentObject<T extends ChildObject> implements Iterable<T> {
        protected List<T> theChildObjects;
    
        public T getChildObjectByIndex(int idx) {
            return theChildObjects.get(idx);
        }
    
        public Iterator<T> iterator() {
            return java.util.Collections.unmodifiableCollection(this.theChildObjects).iterator();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the code below, I'm attempting to write a generic function which takes
Attempting to build a C# NPAPI plugin I have found a tutorial which describes
I am attempting to move some code from one VS2008 project to another. The
I am attempting to rotate some text within a label. I have a cusom
I have to convert a generic object(s) into a NameValueCollection. I am attempting to
Attempting to get Spring internationalization working. I have used classpath:messages basename, created .properties files
I have been attempting to develop an embedded webserver within an application that I
Attempting to build an interface and generics based graph and getting an odd error
I have been attempting to write a reusable generic method for lookups on a
I'm bone new to generics in C#, and I'm attempting to create a method

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.