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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:49:26+00:00 2026-05-22T19:49:26+00:00

Can you please explain why it is possible to do: import java.util.ArrayList; import java.util.List;

  • 0

Can you please explain why it is possible to do:

import java.util.ArrayList;
import java.util.List;

public class Covariance {

    class A {
    }

    class B extends A {
    }

    class C extends A {
    }

    public void testSmth() throws Exception {
        List<? extends A> la = new ArrayList<A>();
        A a = la.get(0);
        // la.add(new B()); - doesn't compile

        List<? super B> lb = new ArrayList<A>();
        // lb.add(new A()); - doesn't compile
        lb.add(new B());
        Object object = lb.get(0);
    }

}

I don’t understand, why it is not possible to add something to covariant list la, but it’s still possible to add B to contravariant list lb – but not A to lb.

From my point of view, it should be possible to add everything extending A to List. I can see the only reason of not doing that because it is easy to add C to list of B, like

 List<B> lst = new ArrayList<B>();
 List<? extends A> lstB = lst;
 lstB.add(C); // this is valid for <? extends B> but list lst would contain instance of B.

Probably the same is true for contravariance, e.g

 List<B> lst = new ArrayList<B>;
 List<? super C> lstC = lst;
 lstC.add(new C());
 Object obj = lstC.get(0);

What I don’t understand – why it is not possible to do

 B b = lstC.get(0);

It is obvious that on this stage super of C would be class B – Java doesn’t allow multiple inheritance.

Also why it prohibits

 lstC.add(new B());

it’s not clear for me.

  • 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-22T19:49:27+00:00Added an answer on May 22, 2026 at 7:49 pm

    To understand what’s going on with the super keyword, consider the following:

    import java.util.ArrayList;
    import java.util.List;
    
    public class Covariance {
    
        class A {
        }
    
        class B extends A {
        }
    
        class C extends A {
        }
    
        class D extends C {}
    
        public void testSmth() throws Exception {
            List<? super D> ld = new ArrayList<C>();
        }
    
    }
    

    Hopefully this illustrates that ld can be a List of any super type of D, even one that be a subclass of A.

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

Sidebar

Related Questions

Can someone please explain why this program outputs 0x00000004? class AndAssignment { static void
Possible Duplicates: class << self idiom in Ruby Can someone please explain class <<
Possible Duplicate: Java Generics Hi, Can anyone please explain the difference of the three
Can someone please explain what the & does in the following: class TEST {
can somebody please explain is it possible to convert this snippet of the code
Can someone please explain in a way as simple as possible what the Model
Can someone please explain to me what is the difference between protected / public
can someone please explain to me why having a doctype of <!DOCTYPE HTML PUBLIC
Possible Duplicate: Memory randomization as application security enhancement? hi, Can some explain me please
Possible Duplicate: Reason for the Output Hi, Can you please explain me the output

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.