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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:32:47+00:00 2026-05-15T08:32:47+00:00

My question is not easy to explain using words, fortunately it’s not too difficult

  • 0

My question is not easy to explain using words, fortunately it’s not too difficult to demonstrate. So, bear with me:

public interface Command<R>
{
    public R execute();//parameter R is the type of object that will be returned as the result of the execution of this command
}

public abstract class BasicCommand<R> implements Command<R>
{
}

public interface CommandProcessor<C extends Command<?>>
{
    public <R> R process(C<R> command);//this is my question... it's illegal to do, but you understand the idea behind it, right?
}

//constrain BasicCommandProcessor to commands that subclass BasicCommand
public class BasicCommandProcessor<C extends BasicCommand<?>> implements CommandProcessor<C>
{
    //here, only subclasses of BasicCommand should be allowed as arguments but these
    //BasicCommand object should be parameterized by R, like so: BasicCommand<R>
    //so the method signature should really be 
    //    public <R> R process(BasicCommand<R> command)
    //which would break the inheritance if the interface's method signature was instead:
    //    public <R> R process(Command<R> command);
    //I really hope this fully illustrates my conundrum
    public <R> R process(C<R> command)
    {
        return command.execute();
    }
}

public class CommandContext
{
    public static void main(String... args)
    {
        BasicCommandProcessor<BasicCommand<?>> bcp = new BasicCommandProcessor<BasicCommand<?>>();
        String textResult = bcp.execute(new BasicCommand<String>()
        {
            public String execute()
            {
                return "result";
            }
        });
        Long numericResult = bcp.execute(new BasicCommand<Long>()
        {
            public Long execute()
            {
                return 123L;
            }
        });
    }
}

Basically, I want the generic “process” method to dictate the type of generic parameter of the Command object. The goal is to be able to restrict different implementations of CommandProcessor to certain classes that implement Command interface and at the same time to able to call the process method of any class that implements the CommandProcessor interface and have it return the object of type specified by the parametarized Command object. I’m not sure if my explanation is clear enough, so please let me know if further explanation is needed. I guess, the question is “Would this be possible to do, at all?” If the answer is “No” what would be the best work-around (I thought of a couple on my own, but I’d like some fresh ideas)

  • 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-15T08:32:47+00:00Added an answer on May 15, 2026 at 8:32 am

    Unfortunately, you cannot do this. Since you want the CommandProcessor interface to be defined in terms of Command, your implemnetation must be prepared to take any kind of Command instance – generics cannot restrict this to BasicCommand – if it could, then the BasicCommandProcessor subclass would not implement the CommandProcessor interface.

    Or, from another angle, given a CommandProcessor interface, it’s not possible for generics to ensure that this was only called with BasicCommand instances. To do this would need to know the implemnetation, and would go against the point of polymorphism and interfaces.

    You can parameterize the result of the command, but not the concrete class of command.

    public interface Command<R>
    {
        public R execute();//parameter R is the type of object that will be returned as the result of the execution of this command
    }
    
    public abstract class BasicCommand<R> implements Command<R>
    {
    }
    
    public interface CommandProcessor
    {
        public <R> R process(Command<R> command);
    }
    
    public class BasicCommandProcessor implements CommandProcessor
    {
        public <R> R processBasicCommand(BasicCommand<R> command)
        {
           return command.execute();
        }
    
        public <R> R process(Command<R> command)
        {
           return processBasicCommand((BasicCommand<R>)command);
        }
    }
    

    The simplest approach is to provide a method that accepts the specific type you need, and call that in the generic method. (See BasicCommandProcessor above.)

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

Sidebar

Ask A Question

Stats

  • Questions 440k
  • Answers 441k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This Railscast provides one possible solution: <% for course in… May 15, 2026 at 5:25 pm
  • Editorial Team
    Editorial Team added an answer The TinyURL service has an incredibly simple API for generating… May 15, 2026 at 5:25 pm
  • Editorial Team
    Editorial Team added an answer It's called context because HelloGridView.this (Activity class) extends the Context… May 15, 2026 at 5:25 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.