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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:31:41+00:00 2026-05-17T01:31:41+00:00

I have the following generic question Here is a my generic message interface. public

  • 0

I have the following “generic” question

Here is a my generic message interface.

public interface Message<P,R> {  
    void setParams(P Params); // and the corresponding getter  
    void setResults(R results);  
}  

For simplicity reasons I would like to specify a number (of interfaces) of messages by predefining the parameter and result type. So that if you work with message X you know what the params and result type are.

Such as

public interface MyMessage extends Message<String,String> {}  

That works fine, now I would like to have a common base class which can implement any of the messages.

public class BaseMessage<P,R> implements Message<P,R> {
    P param;
    R results;
    // base implementation
}

The problem of the above now is that I can’t have a BaseMessage which implements MyMessage.

Multiple attempts like

public class BaseMessage<T extends BaseMessage<P,R>> implements Message<P,R> 

or

public class BaseMessage<? extends BaseMessage<P,R>> implements Message<P,R> 

will not work, as the compiler complains that P and R are not bound.
Introducing them as additional parameters works again, but at that moment the resulting class will not be cast-able to BaseMessage or the desired MyMessage.

public class BaseMessage<P,R,T extends BaseMessage<P,R>> implements Message<P,R> 

I would like to have a generic factory method, which would be given the message class, the parameter and the result object and will return an instance of BaseMessage which implements the MyMessage interface. Like in the follwing

public static <P, R, T extends Message<P,R>> T factory(Class<T> clazz,P p,R r) {
    T a = (T) new BaseMessage<P,R>();
    // create a BaseMessage which which Implements T
    a.setParams(p);
    a.setResults(r);
    return a;
}

Anyone encountered a similar problem, or is that something not possible in Java?

Thanks!

  • 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-17T01:31:42+00:00Added an answer on May 17, 2026 at 1:31 am

    Given the interfaces interface Message<P,R> and interface MyMessage extends Message<String, String> and the class class BaseMessage<P,R> implements Message<P,R>, these are the options I see:

    1. Don’t use MyMessage and replace all instances with Message<String, String>. Your factory will be

      public <P,R> Message<P, R> newMessage() { 
          return new BaseMessage<P,R>();
      }
      
    2. have a class

      class MyBaseMessage extends BaseMessage<String, String> implemens MyMessage
      

      The factory will be

      public <M extends Message> M newMessage(Class<M> m) { 
          if (m.isAssignableFrom(MyBaseMessage.class)) {
              return (M) new MyBaseMessage();
          }
          if (m.isAssignableFrom(BaseMessage.class)) {
              return (M) new BaseMessage();
          }
          return null;
      }
      
    3. Don’t use interfaces.

    4. Don’t use a factory.

    5. (if everything else breaks) Let your factory be

      public <M extends Message> M newMessage(Class<M> m) { 
          try {
              return getImplClass(m).newInstance();
          } catch (Exception ex) { /* do proper error handling */ }
      }
      

      getImplClass(): Use javassist to create an implementation class at runtime that implements the interface M and extends BaseMessage.

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

Sidebar

Related Questions

I have some generic types, like the following: public struct Tuple<T1, T2> { ...
I have following classes. class A { public: void fun(); } class B: public
Following on from my question here , I'm trying to create a generic value
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have a generic question about javascript specification or implementation of functions pointer (delegates?)
I have the following model structure: class Container(models.Model): pass class Generic(models.Model): name = models.CharacterField(unique=True)
I have following situation: I have loged user, standard authentication with DB table $authAdapter
I have following string String str = replace :) :) with some other string;
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
I have following situation. A main table and many other tables linked together with

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.