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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:36:45+00:00 2026-05-18T04:36:45+00:00

Is it possible in Java to create a static factory method/class that uses an

  • 0

Is it possible in Java to create a static factory method/class that uses an interface as the parameterized type and return an implementing class of this given interface?

Although my knowledge of Generics is limited, here is what I want to do:

// define a base interface:
public interface Tool {
    // nothing here, just the interface.
}

// define a parser tool:
public interface Parser extends Tool {
    public ParseObject parse(InputStream is); 
}

// define a converter tool:
public interface Converter extends Tool {
    public ConvertObject convert(InputStream is, OutputStream os);
}

// define a factory class
public class ToolFactory {
    public static <? extends Tool> getInstance(<? extends Tool> tool) {
       // what I want this method to return is:
       // - ParserImpl class, or
       // - ConverterImpl class
       // according to the specified interface.
       if (tool instanceof Parser) {
          return new ParserImpl();
       }
       if (tool instanceof Converter) {
          return new ConverterImpl();
       }
    }
}

I want to restrict the client code to only insert an interface ‘type’ into the getInstance() method that extends from the Tool interface I specified. This way I know for sure that the tooltype that is inserted is a legitimate tool.

Client code should look like this:

public class App {
   public void main(String[] args) {

      Parser parser = null;
      Converter converter = null;

      // ask for a parser implementation (without knowing the implementing class)
      parser = ToolFactory.getInstance(parser);

      // ask for a converter implementation
      converter = ToolFactory.getInstance(converter);

      parser.parse(...);
      converter.convert(... , ...);
   }
}

The factory should switch on the type of the interface (careless if it’s null or not), defined before it is asked from the factory. I know this is not going to work the way I wrote this, but I hope one of the readers knows what I want to accomplish.

The return type of the getInstance method is the same as the incoming parameter, so when a Parser interface is passed, it also returns a Parser p = new ParserImpl(); return p;

Thanks in advance for helping me.

  • 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-18T04:36:45+00:00Added an answer on May 18, 2026 at 4:36 am

    A couple of things:

    1. Your factory should almost certainly take a class to instantiate, rather than a Tool object. Having someone create a Parser to pass into your method in order to get a Parser is a bit chicken-and-egg.
    2. I don’t know if you’re allowed to have generic parameters for methods that are wildcards; I presume not since this would be nonsensical and pointless. When you parameterise a method, you need to give the generic parameter a name so that you can refer to it later on.

    Putting these together, your factory method might look more like this:

    public static <T extends Tool> T getInstance(Class<T> toolClass) {
       if (Parser.class.isAssignableFrom(toolClass) {
          return new ParserImpl();
       }
       else if (Converter.class.isAssignableFrom(toolClass) {
          return new ConverterImpl();
       }
    
       // You'll always need to have a catch-all case else the compiler will complain
       throw new IllegalArgumentException("Unknown class: " + toolClass.getName());
    }
    

    If you want to restrict the type of toolClass to be an interface, you can’t do this at compile-time, but you can of course introduce a runtime check toolClass.isInterface().

    By the way, this static hardcoded switching isn’t very nice in general. To my mind, it would be nicer to put the class-to-constructor relationship in a Map and look up the construction process dynamically. Maybe even store the value as a Callable<? extends Tool> and add a protected method allowing other classes to register mappings.

    That’s not to say that your current version doesn’t work, just that it doesn’t scale very well, and right now I don’t think it’s doing much to justify having a separate factory rather than the caller simply invoking toolClass.newInstance() themselves.

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

Sidebar

Related Questions

is it possible to create a java application that will import excel files .
I am trying to create a utility class that accepts any type of POJO,
I need to create a static object inside a class definition. It is possible
I wonder, whether it is possible to create class-methods in VBA. By class-method I
In Java I had a method like this. public static void digPackageLines( List<PkgLine> pkgLineList,
If it is possible, how can I create a static multidimensional array in Java
Possible Duplicate: Static nested class in Java, why? I know about the static fields
I am trying to implement parameterized class in Java with enum as a type
Possible Duplicate: Can we override static method in Java? We cannot override the static
Is it possible to create in Java an array indexed by letter characters ('a'

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.