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

  • Home
  • SEARCH
  • 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 6033273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:27:35+00:00 2026-05-23T05:27:35+00:00

I’m struggling to come up with an easy API due to generics not being

  • 0

I’m struggling to come up with an easy API due to generics not being covariant

This is my new problem: I can’t get a Set<> that does what I need. I’ve tried reading various guides but they all use lots of buzzwords that I get lost.

Consider the following classes

public class Parent {}
public class Child extends Parent {}
interface Store {
    public Set<Parent> getParents(); //PROBLEM!! This needs to change
}

At a minimum, I need these operations to work

Set<Parent> parents = store.getParents();
parents.add(new Parent()); 
parents.add(new Child()); 
store.getParents().add(new Child()); //Note the lack of generics

for(Parent curEntry : store.getParents()) {

}

Classes that implement Store need to be able to work with Child (meaning they have a Set that is child). They need to expose the Childs as Parents though to the outside world.


Attempt #1

interface Store {
    public Set<Parent> getParents();
}

class ConcreteStore implements Store {
    Set<Child> childs;
    public Set<Parent> getParents() {
        return (Set<? extends Parent>)childs; //ERROR: inconvertible types
    }
}

Attempt #2

interface Store {
    public Set<? extends Parent> getParents();
}

class ConcreteStore implements Store {
    Set<Child> childs;
    public Set<Child> getParents() {
        return childs;
    }
}

Store store = new ConcreteStore();
Set<? extends Parent> parents = store.getParents();
parents.add(new Child()); //ERROR: cannot find symbol
parents.add(new Parent()); //ERROR: cannot find symbol. What?!

for (Parent curEntry : store.getParents()) {
}

That version, while I really like it, means that adding and removing from the Set outside of the Concrete class isn’t possible. Which makes it useless. What really puzzles me is that not even adding a Parent will work.

Attempt #3

interface Store<T extends Parent> {
    public Set<T> getParents();
}

public static class ConcreteStore implements Store<Child> {
    Set<Child> childs;

    @Override
    public Set<Child> getParents() {
        return childs;
    }
}

Store store = new ConcreteStore();
Set<Parent> parents = store.getParents();
parents.add(new Parent());
parents.add(new Child());

for (Parent curEntry : store.getParents()) { //ERROR: incompatible types. Found object, requited Parent
}

Note that here I know I can do Store<Child> store = new DatabaseStore(), but due to a layer of abstraction that isn’t possible and would get lost. Besides, passing around generics everywhere that you use a Parent looks ugly.

—

I’m out of ideas on what to do. Doing this is way more complex than i thought. I really need some way to get

  • 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-23T05:27:36+00:00Added an answer on May 23, 2026 at 5:27 am

    Your problem is unsolvable in the form you pose it.

    • A Set<Parent> is a collection where you can add any Parent object, and all objects you get out of it are Parent objects.
    • A Set<Child> is a collection where you can add any Child object, and all objects you get out of it are Child objects.

    As we can see, there can’t be an object which implements both these interfaces: If you can add any Parent, you can’t be sure to get only Child objects out of it. This means that your ConcreteStore can’t say “I have only childs”, but someone else is allowed to put parents in it.

    The Java generics system is just made to avoid these errors – wherever the compiler barks, you are most probably doing something wrong.

    • A Set<? extends Parent> is a collection of some unknown subtype of Parent. This means that we can’t put anything in it (as we don’t know the right type), and all we can get out of it are Parent objects.

    • A Set<? super Child> is a collection of some unknown supertype of Child. This means that we can put a Child into it, but we can’t be sure what we get out of it (apart from Object, which is the supertype of everything).

    Back to your problem:

    For your operations

    Set<Parent> parents = store.getParents();
    parents.add(new Parent()); 
    parents.add(new Child()); 
    store.getParents().add(new Child()); //Note the lack of generics
    
    for(Parent curEntry : store.getParents()) {
    
    }
    

    to work, you don’t need anything more than what you already posted:

    public class Parent {}
    public class Child extends Parent {}
    interface Store {
        public Set<Parent> getParents();
    }
    

    But now there can’t be store implementations which have only Childs – since you need to be able to add Parents.

    You could make Store a parameterized type instead:

    interface Store<X extends Parent> {
       public Set<X> getParents();
    }
    

    Then you would have

    class ConcreteStore implements Store<Child> {
        Set<Child> childs;
        public Set<Child> getParents() {
            return childs;
        }
    }
    

    Of course, this still will not allow you to put parents in it, but now the callers can see this – and there could be another implementation which implements Store<Parent>, which would allow this.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.