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

  • Home
  • SEARCH
  • 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 803601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:47:09+00:00 2026-05-14T23:47:09+00:00

So strange! Please have a look the code first: public class A {} public

  • 0

So strange! Please have a look the code first:

public class A {}

public class B extends A {}

public class C extends A {}

public class TestMain {

    public <T extends A> void test(T a, T b) {}

    public <T extends A> void test(List<T> a, List<T> b) {}

    public void test1(List<? extends A> a, List<? extends A> b) {}

    public static void main(String[] args) {
        new TestMain().test(new B(), new C());
        new TestMain().test(new ArrayList<C>(), new ArrayList<C>());
        new TestMain().test(new ArrayList<B>(), new ArrayList<C>());
        new TestMain().test1(new ArrayList<B>(), new ArrayList<C>());
    }
}

The statement new TestMain().test(new ArrayList<B>(), new ArrayList<C>()) gets a compilation error:

Bound mismatch: The generic method test(T, T) of type TestMain is not applicable
for the arguments (ArrayList<B>, ArrayList<C>). The inferred type
ArrayList<? extends A> is not a valid substitute for the bounded parameter
<T extends A>

However:

 new TestMain().test(new B(), new C())  --> compiled ok

 new TestMain().test(new ArrayList<C>(), new ArrayList<C>()) --> compiled ok

 new TestMain().test1(new ArrayList<B>(), new ArrayList<C>()) --> compiled ok

If we define the generic before the method name, it seems the type of the second generic List parameter must be the same as that of the first. But there is no restriction if we define generic in parameters.

Is it a feature or a bug of the compile program? Is there some documentation about it?

  • 1 1 Answer
  • 1 View
  • 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-14T23:47:10+00:00Added an answer on May 14, 2026 at 11:47 pm

    There is absolutely no bug; you simply misunderstood the subtyping rules in generics.

    Since we have B extends A:

    • B is a subtype of A
    • an instanceof B is also an instanceof A

    Since Java arrays are covariant:

    • B[] is a subtype of A[]
    • an instanceof B[] is also an instanceof A[]

    However, Java generics are invariant:

    • List<B> is NOT a subtype of List<A>
    • a instanceof List<B> is NOT an instanceof List<A>.

    When you have the following generic method declaration:

    public <T extends A> void test(List<T> a, List<T> b) 
    

    Then, as it’s explicitly stated here, a and b must both have the same type, List<T>, for some capture conversion of type parameter <T extends A>.

    Since List<B> and List<C> are two different types, you can’t mix them as actual arguments for test. Also, even though B and C are subtypes of A, generics are invariant, so neither List<B> nor List<C> is a List<A>.

    Thus,

    test(new ArrayList<B>(), new ArrayList<C>()); // error!!! doesn't compile!!!
    

    doesn’t compile, which is expected behavior.

    See also

    • Java Tutorials/Generics
      • Subtyping
      • More fun with wildcards
    • JLS 10.10 Array Store Exception and java.lang.ArrayStoreException
      • Java array subtyping, being covariant, is not typesafe; generics, being invariant, is.

    Related questions

    On generics typing rules:

    • Any simple way to explain why I cannot do List<Animal> animals = new ArrayList<Dog>()?
    • java generics (not) covariance
    • What is a raw type and why shouldn’t we use it?
      • Explains how raw type List is different from List<Object> which is different from a List<?>

    On using super and extends:

    • Java Generics: What is PECS?
      • From Effective Java 2nd Edition: “producer extends consumer super“
    • What is the difference between super and extends in Java Generics
    • What is the difference between <E extends Number> and <Number>?
    • How can I add to List<? extends Number> data structures? (YOU CAN’T!)

    On actual generic bugs:

    • Generics compiles and runs in Eclipse, but doesn’t compile in javac
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please take a look at this code and run it: I'm getting very strange
Please have a look at this page www.pixeli.ca/issue. I have begun making a page
Strange performance outcome, I have a LINQ to SQL query which uses several let
I have a very strange problem. Under some elusive circumstances I fail to apply
I'm seeing strange errors when my C++ code has min() or max() calls. I'm
Some things look strange to me: What is the distinction between 0.0.0.0, 127.0.0.1, and
I have the following PHP code; <?php component_customer_init(); component_customer_go(); function component_customer_init() { $customer =
Have a look at this page: http://173.203.72.190:12123/Search-Results.aspx?Mode=Browse&Cuisine=-1&Zone=-1 If you click 'Browse' with the 'Any
I have this strange bug with my popup menu. It happens rarely and seemingly
I have done some tests with QueryPerformanceCounter and got strange results. It seems that

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.