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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:44:04+00:00 2026-05-25T20:44:04+00:00

I have the following 2 interfaces accordingly to abstract factory pattern: public interface GenericObjectInterface<T

  • 0

I have the following 2 interfaces accordingly to abstract factory pattern:

public interface GenericObjectInterface<T extends Number>{
    public T getResult();
}
public interface AbstractFactoryInterface{
    public <T extends Number> GenericObjectInterface<T> createGenericObject();
}

I have an abstract class implementing GenericObject, but it’s still unaware of the concrete type (it does only generic operations on Number):

public abstract class GenericAbstractClass<T extends Number> implements GenericObjectInterface<T>{   } 

Then I have a series of concrete class extending that perform generic parameter substitution:

public class IntegerObject extends GenericAbstractClass<Integer>{
     public Integer getResult(){}
}
....

Now, from inside an implementation of the factory I build the concrete type, that’s implementing GenericObjectInterface but has lost it’s generic parameter:

public class ConcreteFactory{
    public <T extends Number> GenericObjectInterface<T> greateGenericObject(Class<T> c){
         if (c.class.isInstance(Integer.class)){
             IntegerObject obj = new IntegerObject();
             //I would like to return obj
             GenericObjectInterface<T> a = new IntegerObject(); //errror
             GenericAbstractClass<T> a = new IntegerObject(); //errror

             return a;
          }else if (c.class.isInstance(Double.class)){
          }
    }
}

I would like to return obj that implements GenericObjectInterface but I don’t know how can I do it.
how can I solve this?

I’m used to abstract factory but I’ve never used it with generics. Am I doing some mistakes in interpreting the pattern?

  • 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-25T20:44:04+00:00Added an answer on May 25, 2026 at 8:44 pm

    If your method returns an IntegerObject why don’t you just return GenericObjectInterface<Integer>? You already know the parameter type.

    In that case, just add a generic parameter to AbstractFactoryInterface, too:

    public interface AbstractFactoryInterface<T extends Number> { ... }
    
    public class ConcreteFactory implements AbstractFactoryInterface<Integer> { ... }
    

    In your implementation the type of T would be inferred from the assignment, and thus you could do this:

     GenericObjectInterface<Double> g = new ConcreteFactory().greateGenericObject();
    

    In that case T would be Double but you’d use Integer internally, resulting in this:

    GenericObjectInterface<Double> a = new IntegerCell(); 
    

    Since the compiler can’t ensure that T will always be of type Integer it won’t allow you to do that assignment.

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

Sidebar

Related Questions

I have the following Interfaces: public interface ITemplateItem { int Id { get; set;
imagine you have the following interfaces: public interface IInterfaceA : IInterfaceX { // //
I have the following interfaces and classes: public interface ILoggingService { ... } public
I have following Classes and interfaces.. public interface ITaggable { ICollection<Tag> Tags { get;
I have the following Java interfaces, public interface JavascriptInterface { public MyIterator getItems(); }
I have the following classes / interfaces: public interface IProjectRepository { IQueryably<Project> GetProjects(); }
I have the following interfaces defined: public interface IAudit { DateTime DateCreated { get;
I currently have interfaces much like the following: interface IService { void Start(); IHandler
Consider I have the following interface: public interface A { public void b(); }
Hi I have the following interfaces that I need to create. Can you just

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.