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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:20:24+00:00 2026-06-15T20:20:24+00:00

public abstract class Mother { } public class Daughter extends Mother { } public

  • 0
public abstract class Mother {
}

public class Daughter extends Mother {
}

public class Son extends Mother {
}

I need a Map which keys are one of Daughter or Son classes, and which values are lists of objects of one of those two classes, respectively.

For example:

/* 1. */ map.put(Daughter.class, new ArrayList<Daughter>()); // should compile
/* 2. */ map.put(Son.class, new ArrayList<Son>()); // should compile
/* 3. */ map.put(Daughter.class, new ArrayList<Son>()); // should not compile
/* 4. */ map.put(Son.class, new ArrayList<Daughter>()); // should not compile

I tried Map<Class<T extends Mother>, List<T>>, but it does not compile.

Map<Class<? extends Mother>, List<? extends Mother>> does compile, but the cases 3. and 4. do compile too while then shouldn’t.

Is it even possible?

  • 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-15T20:20:25+00:00Added an answer on June 15, 2026 at 8:20 pm

    I don’t think it’s possible to encode this in the type, I’d do it with a custom class

    class ClassMap<T> {
      private Map<Class<? extends T>, List<? extends T>> backingMap = new HashMap<>();
    
      public <E extends T> void put(Class<E> cls, List<E> value) {
        backingMap.put(cls, value);
      }
    
      @SuppressWarnings("unchecked")
      public <E extends T> List<E> get(Class<E> cls) {
        return (List<E>)backingMap.get(cls);
      }
    }
    

    It’s safe to suppress warnings here as long as you don’t leak the backingMap reference outside this class.

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

Sidebar

Related Questions

I need a parent public abstract class which will have multiple child public static
classes: public abstract class BaseHolidayPackageVariant { private Integer variantId; private HolidayPackage holidayPackage; private String
public abstract class GenericTests<T extends Number> { protected abstract T getT(); @Test public void
I have this simple interface/class: public abstract class Message {} public class Message1 extends
I have the following abstract classes: public abstract class AbSuperClass1<K,S> { //class definition }
public abstract class ASTNode3 extends ASTNode { ASTNode child1; ASTNode child2; ASTNode child3; public
I have these classes: public abstract class CustomField { public String Id { get;
EventHandler.java: public abstract class EventHandler<E extends EventArgs> { public abstract void HandleEvent(Object sender, E
In an abstract class is the following abstract method definition: public abstract Map<String, String>
I have a class public abstract class FakeClass<T extends MyClass> { protected HashMap<Character, T>

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.