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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:10:51+00:00 2026-06-07T20:10:51+00:00

I know that there a couple of threads for this topic but I could

  • 0

I know that there a couple of threads for this topic but I could not figure out where my problem is. Reading up and down the search results I am more puzzled than ever. I hope I can make my problem clear.

I have a message type hierarchy and I have a generic processor hierarchy for these messages. This I have running so far. Now I want to implement a factory method for the processors and get stuck with the generic approach I have chosen. Please have a look at my example:

abstract class Message
{
    abstract int getTest();
}

class MyMessage1 extends Message
{
    @Override
    int getTest()
    {
        return 1;
    }
}

class MyMessage2 extends Message
{
    @Override
    int getTest()
    {
        return 2;
    }
}

interface Processor<T extends Message>
{
    void initialize(final T p_message);

    void doWork();
}

abstract class BaseMessageProcessor<T extends Message> implements Processor<T>
{
    private T message;

    @Override
    public void initialize(T p_message)
    {
        message = p_message;
    }

    protected T getMessage()
    {
        return message;
    }
}

class MyMessage1Processor extends BaseMessageProcessor<MyMessage1>
{
    @Override
    public void doWork()
    {
        // perfectly valid assignment
        MyMessage1 msg = getMessage();
        // do something
    }
}

class MyMessage2Processor extends BaseMessageProcessor<MyMessage2>
{
    @Override
    public void doWork()
    {
        // perfectly valid assignment
        MyMessage2 msg = getMessage();
        // do something
    }
}

So far this is valid and working as expected. But now comes the factory:

class ProcessorFactory
{
    <T extends Message> Processor<T> createProcessor(final Class<T> p_msgType)
    {
        if (p_msgType.equals(MyMessage1.class))
        {
            // Type mismatch: cannot convert from MyMessage1Processor to Processor<T>
            return new MyMessage1Processor();
        }
        else if (p_msgType.equals(MyMessage2.class))
        {
            // Type mismatch: cannot convert from MyMessage2Processor to Processor<T>
            return new MyMessage2Processor();
        }
        else
        {
            throw new Exception("Unsupported message type: " + p_msgType);
        }
    }
}

Maybe it’s a stupid failure I am making here but I can’t see it. If someone could give me a hint I would be thankful.

Regards

Sebastian

EDIT:
OK my fault. The problem is that I get the compiler errors stated as comments as ‘default locale’ mentioned (e.g., for the second return statement I get the compile error):

Type mismatch: cannot convert from MyMessage2Processor to Processor

  • 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-07T20:10:52+00:00Added an answer on June 7, 2026 at 8:10 pm

    To understand why you are failing, you have to understand how type erasure works:

    While you clearly know you are doing things right, the compiler doesn’t and, most importantly, can’t know it, for design, because the information has been cancelled

    Let me try to further explain this concept: because of type erasure, the type T is turned at compile time into a java.lang.Object and there is no compile-time information for the Java compiler that says that

    if a.getClass().equals(MyClass.class) then a.getClass() will be of type MyClass<MyClass>


    Important: please avoid to write this code as much as you can because that would lead to horrible design. If you do not want to die in the hell of bugs and unmaintanable code, do not force type-safe languages to use logical evidence of type safety and not compile type evidence. If you forces the type safety by casts in the compiler, you risk that one day you change the class structure and you forget to change the factory, everything compiles fine but at runtime you get a BOOM.

    A factory method is in generally a pattern for hiding from the caller the logic of producing an object and if you end up with such a situation, it probably means that you are not using generics correctly. It is much better to write one more class and having a clean design.

    Maybe you can add an additional question on : how to implement this pattern in a type-safe way?

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

Sidebar

Related Questions

I know that there are lots of examples out there on this, but they
I know that there are some threads have a similar issue with this thread.
I know that there are lot's of questons on this, but all seem to
I know that there can be multiple values for an email, but I'm not
I know that there are couple of questions here about a countdown timer but
I realize that there are a couple other posts on this topic, however the
I know that there are tons of articles about this issue. But I'm struggling
I know that there are others posts with solutions on the site but i
I know that there are many Delphi database related questions available, but I'm considering
i know that there is some rules and standards in css handling but i

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.