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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:12:33+00:00 2026-05-11T17:12:33+00:00

Imagine a generic class MySet which maintains a parent MySet instance and a child

  • 0

Imagine a generic class MySet which maintains a parent MySet instance and a child MySet instance. The idea is that the parent should be able to hold a superset of T and the child a subset. So given the following sample, consider the following problem:

class MySet<T> {

  MySet<? extends T> child; 

  void doStuff (Collection<? extends T> args) {
    child.doStuff(this, args);
  }

}

EDIT: fixed question and sample code to reflect the real problem

Now, the child generic <T> may be more restrictive than the parent’s <T>, so the parent must pass in a Collection<X> where <X> conforms to the child’s <T>. Keep in mind that this parent->child chain could extend to be arbitrarily long. Is there any way to arrange the generics so that parent.doStuff(…) will compile, ie, so that it can only be called with the arguments of it’s most restrictive child?

This would mean that the java compiler would pass up generic information all the way up the parent->child chain to determine what the allowable arguments to doStuff could be, and I don’t know if it has that capability.

Is the only solution to ensure children cannot be more restrictive than their parents using generics (ie, MySet<T> child; rather than MySet<? extends T>) and have children be more restrictive than their parents elsewhere in the code?

  • 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-11T17:12:33+00:00Added an answer on May 11, 2026 at 5:12 pm

    I can give a negative answer to part of that question right away:

    Is there any way to arrange the
    generics so that parent.doStuff(…)
    will compile, ie, so that it can only
    be called with the arguments of it’s
    most restrictive child?

    This would mean that the java compiler
    would pass up generic information all
    the way up the parent->child chain to
    determine what the allowable arguments
    to doStuff could be, and I don’t know
    if it has that capability.

    The simple answer is no, because the actual extend (and type requirements) of that chain are only known at run time, and by then any information about generics has been lost through erasure.

    To push it even further, if you had a checked type instead of vanilla generics, with a method that could pass up in the chain the (most restrictive) actual type accepted, you could do a check at runtime, and raise a runtime error, but it will never be a compile error.

    So no, unless the actual final type is known (and specified, maybe by a second type arg) in advance, the compiler is not going to be able to help you there.

    What you can (and should do imo) is keep with what you just wrote (which should compile just fine), and pass it an argument that can be unsafe. Whether you type-check it yourself, or let the JVM raise a ClassCastException is needed, remains a matter of choice.

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

Sidebar

Ask A Question

Stats

  • Questions 214k
  • Answers 214k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think you need to capitalize "True" and "False." I… May 12, 2026 at 10:46 pm
  • Editorial Team
    Editorial Team added an answer You can debug classic ASP in Visual Studio 2008. The… May 12, 2026 at 10:46 pm
  • Editorial Team
    Editorial Team added an answer The major keyword here is Ajax. It basically boils down… May 12, 2026 at 10:46 pm

Related Questions

I have a problem in a Windows Forms application with Bitmap.Save failing when I
I've came across a dilemma which I think is worth discussing here. I have
On C# 3.0 and .NET 3.5, imagine there's an interface: public interface INameable {
Kind of theoretical question. Quite long so feel free to skip if you are

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.