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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:26:59+00:00 2026-06-10T23:26:59+00:00

Today I ran into the following problem. Consider the setup: interface A { void

  • 0

Today I ran into the following problem. Consider the setup:

interface A {
    void foo();
}

interface B {
    void bar();
}

class Impl implements A,B {

    public void foo() { }
    public void bar() { }

}

class Usage {

    void worksAsParameter(){
        acceptIt(new Impl());
    }

    <T extends A & B> void acceptIt(T foo){

    }

    <T extends A & B> T returnIt(){
        return new Impl(); // <-- Compile error
    }

}

The code compiles except in the last statement as marked. Eclipse gives me the error: Type mismatch: cannot convert from Impl to T

My question is: Why is Impl assignable to T when given as a parameter (shown in worksAsParameter but not when T is a return type ?
And also, what expression aside from null will satisfy the type T in the case where Impl does not?

Please note that this question is not the same as this SO question although similar.

Edit: Fixed typo.

=== Summary ===
It would seem I had misunderstood how generic return types work. I will try to write up my new understanding of it.
Lets look at the issue:

<T extends A & B> T returnIt(){
    return new Impl(); // <-- Compile error
}

My initial assumption was that the implementing class (in this case Usage) decided on the concrete type for T with the restriction that it must extend A and B. Apparently it is the caller/callsite that gets to decide what T is and Usage must supply a value that is assignable to T. However, as T is a compile time deal only it is impossible to supply such a value aside from null (as it is assignable to anything).
Afaik this means that any code of the form will only ever be able to return null:

<T extends A> T returnIt(){
    return x; // <-- Compile error
}

A fairly unintuitive feature that is hopefully more useful in a different setting. Thanks Peter!

  • 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-10T23:27:00+00:00Added an answer on June 10, 2026 at 11:27 pm

    The reason

    <T extends A & B> T returnIt(){
        return new Impl();
    }
    

    doesn’t compile is that T could be any class which extends A and B. You happen to know there is only one possible class at the moment, but the compile doesn’t “know” this.

    e.g.

    class AB extends A, B { }
    
    Usage usage = ...
    AB ab = usage.<AB>returnIt(); // T is AB not Impl.
    

    You can force the issue with

    <T extends A & B> T returnIt(){
        return (T) new Impl(); // unchecked cast warning.
    }
    

    but a better solution is

    Impl returnIt(){
        return new Impl();
    }
    

    This defines two generics

    <T extends A, B>
    

    T extends A and also B extends Object

    What you may have intended is

    <T extends A & B>
    

    where T must extend A and B.

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

Sidebar

Related Questions

Today I ran into the following problem with NUnit. I have a class, that
I ran into a problem today trying to override an implementation of an interface
I ran into a problem today at work wherein I have a BindingGroup that
Ran into a strange problem in PHP today and I'm wondering if someone can
I ran into a strange problem today, after renaming a couple of images in
I ran into a little problem today when I was creating a really quick
I ran into a interesting issue today. Check out this pseudo-code: void Loop() {
I ran into a peculiar problem today when I wanted to migrate the history
Today I ran into a very difficult TDD problem. I need to interact with
today I ran into this problem which really bugs me, as almost the code

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.