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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:37:17+00:00 2026-05-19T02:37:17+00:00

EDIT: There is another wrinkle in here that I missed that turns out to

  • 0

EDIT: There is another wrinkle in here that I missed that turns out to make a big difference. The method signature of doAnotherThing is instead the following:

<T extends Bar> T doAnotherThing(List<Foo<T>> foo) {
    return foo.get(0).doSomething();
}

Disregard the fact that it is a List, just pay attention to the fact that List is a generic class/interface. I’m calling the method like so:

doAnotherThing(new ArrayList<FooImpl>);

So, I have a class and interface defined like so:

abstract class Bar {
    // some neat stuff
}

class BarImpl extends Bar {
    // some cool stuff
}

interface Foo<T extends Bar> {
    T doSomething();
}

class FooImpl implements Foo<BarImpl> {
    BarImpl doSomething() {
        // Does something awesome
    }
}

This is all fine and dandy and works great.

Now, I have a method like so:

<T extends Bar> T doAnotherThing(List<Foo<T>> foo) {
    return foo.get(0).doSomething();
}

This method is a generic method in a completely different class and is not part of the chain above.

However, when I try to use this method in the following manner, I’m getting an error saying that the types don’t match:

doAnotherThing(new FooImpl());

FooImpl implements Foo<T>, so I don’t see how this is an error? Maybe I’m misunderstanding something? 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-19T02:37:18+00:00Added an answer on May 19, 2026 at 2:37 am

    Answer changed to reflect clarification in question

    The complilation error (type mismatch) is actually perfectly correct. Note that the method definition says the parameter is a List<Foo<T>>. This means that the list can contain any Foo<T>, and the method must even be able to add any object that implements Foo<T>. And that’s not the case when you give it a List<FooImpl>, because that is only allowed to contain instances of FooImpl. This works:

    doAnotherThing(new ArrayList<Foo<BarImpl>>());
    

    Mixing generics and polymorphism quickly leads to very complex scenarios, so it should be done sparingly.

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

Sidebar

Related Questions

**EDIT: There are several options below that would work. Please vote/comment according to your
EDIT: There must be some way I can approach this without writing a whole
EDIT: There's now a doc page on this so this question is irrelevant, also
EDIT: There is the almost same question at Removing whitespace between HTML elements when
EDIT - there are no blank lines. EDIT 2 - my bad, it seems
Edit Since there were many downvotes and people who didn't understand what I'm asking
Edit I think there is some confusion, I am not using both of the
Is there any possible way to edit this code <asp:LoginName ID=LoginName1 runat=server style=top: 165px;
Is there a tool where you can edit a .NET dll directly? .net reflector
I have a list of people and there are 4 types that can occur

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.