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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:10:46+00:00 2026-05-14T15:10:46+00:00

I’ve a collection type: Collection<A> collecA And I’ve a list in my object: List<B>

  • 0

I’ve a collection type:

Collection<A> collecA

And I’ve a list in my object:

List<B> listB

Where B is extending A

class B extends A { ... }

But I can’t do the following:

collecA = listB

I can’t understand why since Collection is implemented by List.

  • 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-14T15:10:46+00:00Added an answer on May 14, 2026 at 3:10 pm

    Let’s assume for a moment you could do what you describe:

    class B extends A { ... }
    
    Collection<A> collecA;
    List<B> listB;
    
    collecA = listB;  // normally an error, but lets pretend its allowed
    
    collecA.add(new A()); // PROBLEM!
    

    The method call collecA.add(new A()) appears okay since collecA is a collection
    that holds As. However, if the above assignment were allowed, then we have a
    problem becausecollecA is really reference to a List<B> instance – I just
    added an A into a list that can only hold Bs!

    Asker also said:

    I can’t understand why since Collection is implemented by List.

    It doesn’t matter that Collection is a superclass of List. This assignment is illegal even if you used two lists.

    class B extends A { ... }
    List<A> listA;
    List<B> listB;
    listA = listB;  // still an error, still leads to the same problem
    

    The key is that the List<A> variable can reference only Lists that can hold As. However, a List<B> instance cannot hold As. Therefore, a List<A> variable like listA cannot be assigned a reference to a List<B> instance referred to bylistB.

    Or more generally speaking: B being a subclass of A does not imply that SomeGenericClass<B> is a subclass of SomeGenericClass<A> (JLS §4.10: Subtyping does not extend through generic types: T <: U does not imply that C<T> <: C<U>.)


    It was this example/analogy from the Java Generics Tutorial that helped me understand this:

    http://java.sun.com/docs/books/tutorial/java/generics/subtyping.html

    “Understanding why becomes much easier if you think of tangible objects — things you can actually picture — such as a cage:

    // A cage is a collection of things, with bars to keep them in.
    interface Cage<E> extends Collection<E>;
    ...
    Cage<Lion> lionCage = ...;
    Cage<Butterfly> butterflyCage = ...;
    

    But what about an “animal cage”? English is ambiguous, so to be precise let’s assume we’re talking about an “all-animal cage”:

    Cage<Animal> animalCage = ...;
    

    This is a cage designed to hold all kinds of animals, mixed together. It must have bars strong enough to hold in the lions, and spaced closely enough to hold in the butterflies.
    …
    Since a lion is a kind of animal (Lion is a subtype of Animal), the question then becomes, “Is a lion cage a kind of animal cage? Is Cage<Lion> a subtype of Cage<Animal>?”. By the above definition of animal cage, the answer must be “no”. This is surprising! But it makes perfect sense when you think about it: A lion cage cannot be assumed to keep in butterflies, and a butterfly cage cannot be assumed to hold in lions. Therefore, neither cage can be considered an “all-animal” cage:

    animalCage = lionCage;  // compile-time error
    animalCage = butterflyCage; // compile-time error
    

    “

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

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.