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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:48:18+00:00 2026-06-15T21:48:18+00:00

Here is what I want to do: I have an interface Service as follows:

  • 0

Here is what I want to do:

I have an interface Service as follows:

public interface Service<A extends Object, R extends Object> {
    R run(A a);
}

Now, I want to link two such services, while keeping it generic:

public class Link<A, R> implements Service<A, R> {

    private Service<A, X> s1; // fail
    private Service<X, R> s2; // fail

    public <X> Link(Service<A, X> s1, Service<X, R> s2) {
        this.s1 = s1;
        this.s2 = s2;
    }

    @Override
    public R run(A a) {
        return s2.run(s1.run(a));
    }
}

The problem that I want to address is to assign a generic type X to class Link, but I want that to be inferred through its constructor and not through the class declaration.

How is it achievable?

Edit:

The way I’ve solved this is by a utility static method which does the linking process:

public static <A, I, R> Service<A, R> link(final Service<A, I> s1, final Service<I, R> s2) {
    return new Service<A, R>() {
        @Override
        public R run(A a) throws Exception {
            I intermediate = s1.run(a);
            return s2.run(intermediate);
        }
    };
}
  • 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-15T21:48:19+00:00Added an answer on June 15, 2026 at 9:48 pm

    You cannot have a generic type X that is part of your class (not selected anew for each method call) that is not part of its declaration.

    Sure, you could hide the unsafe casts and be certain your program would work, but those are really your only two options in the limitations of Java’s type system.

    What I would do, though, is instead of providing a Link constructor, provide the static factory method

    public static <A, X, R> Service<A, R> link(Service<A, X> s1, Service<X, R> s2) {
      return new Link<A, X, R>(s1, s2);
    }
    

    …ensuring that X is automatically inferred and never escapes.

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

Sidebar

Related Questions

I have a base repository contract which other contracts extend, like follows public interface
Let us say I have a Service public interface IAreaService { int CalculateArea(int x,int
Placing an image inside a Span (here : HtmlGenericControl ) programmatically Want to have
I want to have a page redirect to another. Here is the situation: I
Here i want to do UDP Socket programming in C. I have one device
I want to have a UIImage as a background image on a UIView. Here
To explain my question I have created this http://jsfiddle.net/Jams/XNVB2/ here I want when div1
James here. I'm working on a site that I want to have the same
I work for a travel agency, they have a website, from here they want
Here is what I want to achieve: I have RadGridView, let's call it gridView,

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.