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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:07:20+00:00 2026-06-13T12:07:20+00:00

In java one can cast instance of a class B into an instance of

  • 0

In java one can cast instance of a class B into an instance of class A provided that B extends A.

Can this be done also for type-parametrized classes? For instance, for B extending A, I’d like to do the following:

List<A> l = new ArrayList<B>();

I think this should be legal, but the compiler doesn’t agree with me on this point, so I tricked it with the following hack:

List<A> l = (List<A>)(List) new ArrayList<B>();

…but I think that I’ll be hit by a velociraptor. Is there an elegant way of doing this?

  • 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-13T12:07:22+00:00Added an answer on June 13, 2026 at 12:07 pm

    I think this should be legal, but the compiler doesn’t agree with me on this point

    Generally, when humans and compilers disagree, it is safe to take the compiler’s side. Instead of tricking the compiler, you should first understand why it does not allow the conversion.

    Consider this: you make a List<B>, like this:

    List<B> listB = new ArrayList<B>();
    

    Everyone is happy so far. Now let’s do what you think is correct:

    // This does not compile, but let's pretend that it does
    List<A> listA = (List<A>)listB;
    

    At this point, we have listA that is entirely ours! Let’s play with it: let’s say we have another class C extending A. Can we do this?

    listA.add(new C()); // Why not? The compiler should allow it!
    

    But wait, do you see what we have done? Our listB contains a C now! This is why the compiler does not allow the conversion; this is also why your code is not safe in cases when you attempt to add anything to the list.

    To work around this issue, you can use wildcards: List<? extends A>. However, this will limit your lists to read-only access. If you must allow writing (and you know that you are going to set only the right stuff) use the non-generic List instead.

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

Sidebar

Related Questions

In Java, how can you make an ArrayList read-only (so that no one can
How can one add two long values in Java so that if the result
I just learned (the hard way) that Java Component s can only have one
i want to know is there anyway that using Java one can capture the
Hi have one class like this import java.util.ArrayList; public class MobilePhone { private String
In my Java program, I need to cast an object of type List<? extends
Can any one give me java code to the below emulator images needs? The
Can any one explain how Java SimpleTimeZone rules are interpreted. For example a rule
can any one tell me how to implement a standalone java client for playing
Can any one provide me code for consuming webservices via SOAP in java? Actually

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.