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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:05:48+00:00 2026-05-27T02:05:48+00:00

I have a generic abstract class Factory<T> with a method createBoxedInstance() which returns instances

  • 0

I have a generic abstract class Factory<T> with a method createBoxedInstance() which returns instances of T created by implementations of createInstance() wrapped in the generic container Box<T>.

abstract class Factory<T> {
    abstract T createInstance();

    public final Box<T> createBoxedInstance() {
        return new Box<T>(createInstance());
    }

    public final class Box<T> {
        public final T content;

        public Box(T content) {
            this.content = content;
        }
    }
}

At some points I need a container of type Box<S> where S is an ancestor of T. Is it possible to make createBoxedInstance() itself generic so that it will return instances of Box<S> where S is chosen by the caller? Sadly, defining the function as follows does not work as a type parameter cannot be declared using the super keyword, only used.

public final <S super T> Box<S> createBoxedInstance() {
    return new Box<S>(createInstance());
}

The only alternative I see, is to make all places that need an instance of Box<S> accept Box<? extends S> which makes the container’s content member assignable to S.

Is there some way around this without re-boxing the instances of T into containers of type Box<S>? (I know I could just cast the Box<T> to a Box<S> but I would feel very, very guilty.)

  • 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-27T02:05:48+00:00Added an answer on May 27, 2026 at 2:05 am

    Try rewriting your other code to not use Box<S> but instead uses Box<? extends S>, so it will accept a Box<T>, too. This way, you make it explicit that the Box may also contain subclasses of S.

    The following should also work, if you make your Box static:

    public static <S, T extends S> Box<S> createBoxedInstance(Factory<T> factory) {
      return new Box<S>(factory.createInstance());
    }
    

    However, it may be unable to do type inference for S, at which point you need:

    public static <S, T extends S> Box<S> createBoxedInstance(Factory<T> factory, S dummy) {
      return new Box<S>(factory.createInstance());
    }
    

    Another variation that is very explict:

    public static <S, T extends S> Box<? extends S> createBoxedInstance(Test<T> factory, S dummy) {
      return factory.createBoxedInstance(); // Actually a Box<T>
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a generic object factory FactoryBase<T> with a factory method: public abstract class
I have a abstract generic class. I want to define a method inside there
I have a generic abstract UserControl class, SensorControl , which I want all my
I have a generic abstract base class from which I would like to derive
I have an abstract generic class public abstract class Foo<TType> with an abstract method
I have an abstract generic class BLL<T> where T : BusinessObject . I need
I have an abstract generic class. public abstract class FieldHandlerWithData<DataType extends Parcelable> extends FieldHandler
How can I have a generic abstract class, let's say Animal, and have it
I have a abstract class which represents an immutable value. Like strings, the immutable
In a proof-of-concept project I'm working on, I have a generic abstract class that

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.