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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:26:07+00:00 2026-06-12T10:26:07+00:00

I have some code similar to this: public class Main { private static abstract

  • 0

I have some code similar to this:

public class Main {

    private static abstract class Bar {}

    private static class SubBar extends Bar {}

    private static abstract class Baz<T extends Bar> {

        private T t;

        public void setT(T t) {
            this.t = t;
        }

    }

    private static class SubBaz extends Baz<SubBar> {}


    private void foo(Baz<? extends Bar> baz, Bar bar) {
        baz.setT(bar);
    }
 }

That results in error:

error: method setT in class Baz<T> cannot be applied to given types;
required: CAP#1
found: Bar
reason: actual argument Bar cannot be converted to CAP#1 by method invocation conversion
where T is a type-variable:
T extends Bar declared in class Baz
where CAP#1 is a fresh type-variable:
CAP#1 extends Bar from capture of ? extends Bar

I don’t understand why. The method setT should accept something that extends Bar and I am passing something of class Bar.

  • 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-12T10:26:09+00:00Added an answer on June 12, 2026 at 10:26 am

    The method setT should accept something that extends Bar and I am passing something of class Bar.

    That’s exactly the problem: <? extends Bar> means “Some unknown type that is Bar or a subclass of it”. Since you don’t know which type it is, it’s actually impossible to call setT() in that context, except with a null parameter.

    This will work as expected:

    private void foo(Baz<Bar> baz, Bar bar) {
        baz.setT(bar);
    }
    

    There are, I am sure, hundreds of variations of this questions on Stackoverflow. It seems almost every programmer at first misunderstands what the ? wildcard is for and uses it wrongly.

    Its utility is in the situation where your Bar class has a public T getT() method. A variable of type Baz<? extends Bar> could hold objects of both Baz<Bar> and Baz<SubBar>, and you could call getT() on it to get something that is a Bar (or some subclass) and can be used like that.

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

Sidebar

Related Questions

Let's assume we have some code similar to this: [XmlRoot(class-a)] public class ClassA {
I have some Java code similar to: public class Thing { private IPolicy policy;
I have some code similar to the following: public interface IMyClass { MyEnum Value
I have a some simple Java code that looks similar to this in its
I have a problem with This code in C# : main class { var
I have a some code that gets passed a class derived from a certain
My question somewhat overlaps with this and several other similar ones. Those have some
Long story short Say I have the following code: // a class like this
I have some code for you guru's to check. I know this is probably
I have some code on two systems running kernel 2.4.20 and kernel 2.4.38 .

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.