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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:38:42+00:00 2026-06-16T18:38:42+00:00

Possible Duplicate: Java ArrayList of ? extends Interface There is this code: public static

  • 0

Possible Duplicate:
Java ArrayList of ? extends Interface

There is this code:

public static class B1 {}
public static class B2 extends B1 {}
public void fun() {
    List<? extends B1> l3 = new ArrayList<>();
    l3.add(new B2());
}

Compile error:

java: no suitable method found for add(Main.B2)
    method java.util.List.add(int,capture#1 of ? extends Main.B1) is not applicable
      (actual and formal argument lists differ in length)
    method java.util.List.add(capture#1 of ? extends Main.B1) is not applicable
      (actual argument Main.B2 cannot be converted to capture#1 of ? extends Main.B1 by method invocation conversion)

I guess that ? extends B1 means any type that extends from B1. It seems that type B2 extends from B1, so why object of this type cannot be added to the list and how to make it so that it can be added?

  • 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-16T18:38:43+00:00Added an answer on June 16, 2026 at 6:38 pm

    I guess that ? extends B1 means any type that extends from B1.

    No. It means “a specific, but unknown, type that extends from B1”. As the specific type is unknown, the compiler cannot enforce it, therefore operations like add don’t work.*

    See the tutorial on wildcards.

    how to make it so that it can be added?

    Basically, don’t use wildcards for this. You probably want this:

    List<B1> l3 = new ArrayList<B1>();
    

    * Well, they do work, but only for null (and a few other cases, see @Marko’s comment below).

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

Sidebar

Related Questions

Possible Duplicate: Java: Global Exception Handler I thought of something like this: public static
Possible Duplicate: Why should the interface for a Java class be prefered? ArrayList<Integer> al
Possible Duplicate: How to convert List<Integer> to int[] in Java? I have an ArrayList
Possible Duplicate: Type List vs type ArrayList in Java Hi I have been asked
Possible Duplicate: java arraylist to store userinput Hi using the code in java how
Possible Duplicate: Adding enum type to a list I have an enum class: public
Possible Duplicate: Java static class initialization Why is the string variable updated in the
Possible Duplicate: Java: How to convert List to Map I have arrayList ArrayList<Product> productList
Possible Duplicate: java.util.ConcurrentModificationException on ArrayList I am trying to remove items from a list
Possible Duplicate: Why should the interface for a Java class be prefered? I m

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.