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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:33:17+00:00 2026-06-11T07:33:17+00:00

So I got an interface SuperType and a bunch of implementing classes TypeA ,

  • 0

So I got an interface SuperType and a bunch of implementing classes TypeA, TypeBand so on. I also got a top-level interface which has a parameterized method:

public interface UsedByProductThing<T extends SuperType> {
    public T doStuff(T one);
}

I got a factory (see below) producing objects implementing GeneralProduct:

public interface GeneralProduct<T extends SuperType> {
    T doSomething(T input);
}

Here is the implementing ProductA:

public class ProductA implements GeneralProduct<TypeA> {
    UsedByProductThing<TypeA> in;

    public ProductA(UsedByProductThing<TypeA> in) {
        this.in = in;
        in.doStuff(new TypeA());
    }

    @Override
    public TypeA doSomething(TypeA input) {
        return null;
    }
}

And now the factory in question:

public class GeneralFactory {
    public static <T extends SuperType> GeneralProduct<T> createProduct(
            int type, UsedByProductThing<T> in) {
        switch (type) {
        case 1:
            return (GeneralProduct<T>) new ProductA((UsedByProductThing<TypeA>) in);
            // at this point, i want to return a "new ProductA(in)" preferably
            // without casting
            // or at least without the cast of the argument.
        default:
            throw new IllegalArgumentException("type unkown.");
        }
    }
}

As commented, I want that factory-method to not use a cast. I understand that the return type has to be GeneralProduct, but I can’t think of a way omitting the cast (and it gives me an “unchecked cast”-warning, too). Also, I can’t think of a way omitting the cast of the argument. I’m able to restructure the whole code if it’s necessary to get rid of the “unsafe” casting at that place. Can you tell me a way that would be nice and smooth here?

Also, please edit my question as you like – I don’t know how to adress the issue correctly in the title.

Thanks a lot!

  • 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-11T07:33:18+00:00Added an answer on June 11, 2026 at 7:33 am

    You can’t avoid casting because

    • you have in which has type UsedByProductThing<T> which you want to turn into a UsedByProductThing<TypeA> and the compiler has no way of knowing that T is TypeA
    • ProductA is GeneralProduct<TypeA> and again the compiler doesn’t know that T is a TypeA here either.

    The only way to avoid cast is to replace T with TypeA

        public static GeneralProduct<TypeA> createProduct(
                int type, UsedByProductThing<TypeA> in) {
            switch (type) {
                case 1:
                    return new ProductA(in);
                default:
                    throw new IllegalArgumentException("type unkown.");
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got an IModelElement interface, and several classes implementing that interface. Somewhere in my
(C++) I've got a number of Entry classes, and got BaseProcessor interface which incapsulates
I've got an interface which i've used StructureMap to Dependency Inject . public interface
I've got a class which implements the TableModel Interface. When I call setModel on
I've got WebView, JS code inside it. Also I have interface to allow Java
I got all the classes that implement my interface. I want to call a
I've got an interface inheritance issue that has been vexing me for some time.
I've got a web interface from which I will gather user data (username,pass,email,etc..) and
I've got an interface interface IModule { public function Install(); } and some classes
I've got an interface that two classes implement at the moment. The data for

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.