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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:03:58+00:00 2026-05-15T08:03:58+00:00

In Java, assume I have the following class Container that contains a list of

  • 0

In Java, assume I have the following class Container that contains a list of class Items:


    public class Container<T>
    {
        private List<Item<? extends T>> items;

        private T value;

        public Container(T value)
        {
            this.value = value;
        }

        public void addItem(Item<? extends T> item)
        {
            items.add(item);
        }

        public void doActions()
        {
            for (Item<? extends T> item : items)
            {
                item.doAction(value);
            }
        }
    }

    public abstract class Item<T>
    {
        public abstract void doAction(T item);
    }

Eclipse gives the error:
The method doAction(capture#1-of ? extends T) in the type Item is not applicable for the arguments (T)

I’ve been reading generics examples and various postings around, but I still can’t figure out why this isn’t allowed. Eclipse also doesn’t give any helpful tips in its proposed fix, either. The variable value is of type T, why wouldn’t it be applicable for ? extends T?.

  • 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-15T08:03:58+00:00Added an answer on May 15, 2026 at 8:03 am

    Take a look at the following program

    public class Cell<T> { 
      private T value;
    
      public void set(T t) { value = t; }
      public T get() { return value; }
    }
    
    
    Cell<Integer> ci = new Cell<Integer>();
    Cell<? extends Number> cn = ci;
    
    cn.set(new Double(5.0));  // (A) <-- Compile error here
    
    
    Integer n = ci.get(); // (B) Runtime error!  
    

    As you said, the (A) line does not compile. If this line were legal then, at runtime, the program will pass a Double object to the cn.set() call where cn‘s dynamic type is Cell<Integer>.

    When execution subsequently arrives at (B), ci.get() will return a Double object—the one that was passed in (A)—were the declaration of ci says that its get() method is guaranteed to return an Integer. In order to prevent this conundrum (which actually breaks the JVM’s strong typing philosophy), the compiler disallows assignment from T to <? extends T>.

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

Sidebar

Related Questions

Let's assume I have a file named Main.java with the following code: public class
Let's assume we've got the following Java code: public class Maintainer { private Map<Enum,
I have a quick question about Java synchronization. Please assume the following code: public
I have a quick question about Java synchronization. Please assume the following code: public
A java class does something like the following public class Foo { private final
I have to assume that the following method doesn't leak memory: public final void
Assume you have several arbitrary classes like below: class Foo { $foovar; public function
I want to implement a 'favorite list' and let's assume that I have got
In thinking in Java, page 566 gives the following example. class CountedInteger { private
Let's assume I have a framework written in Java and some C++ code which

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.