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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:29:03+00:00 2026-05-25T00:29:03+00:00

How can I specify that I want any object that implements the A interface

  • 0

How can I specify that I want any object that implements the A interface to be allowed inside of the List in a type-safe manner? I want to be able to invoke a given method of A over all of the objects, regarding what Class type they are.

public interface A (){}

List<? extends A> myList = new ArrayList<? extends A>();

I am getting: Cannot instantiate the type ArrayList<? extends A>

If I leave the generic off of the instantiation side it works fine, but I have to specify @SuppressWarnings({ "rawtypes", "unchecked" }) to make the compiler hush up about the warnings.

  • 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-25T00:29:04+00:00Added an answer on May 25, 2026 at 12:29 am

    To see why it doesn’t make sense to instantiate a new ArrayList<? extends A>(), in fact we can equivalently create a new ArrayList<B>() with any arbitrary class or interface B (even one that is not related to any of your objects) that implements A, and the resulting created ArrayList can be safely assigned to a variable of type ArrayList<? extends A>. Then you might wonder, oh, this is stupid, because the objects I want to put into it are not of class B, so how can this be the same? But it is the same, because you cannot put anything into a reference of type Something<? extends A> anyway. This is why it doesn’t make sense to create a new ArrayList<? extends A>().

    How can I specify that I want any object that implements the A
    interface to be allowed inside of the List in a type-safe manner? I
    want to be able to invoke a given method of A over all of the objects,
    regarding what Class type they are.

    That’s exactly what ArrayList<A> is. It allows all instances of A (of course all objects that implement A are instances of A) and you can use any methods of A on them. Something like this is what you want:

    List<A> myList = new ArrayList<A>();
    

    Something like List<? extends A> is only used in cases when you will get a List object from some other code, and you don’t know (or care) what exactly is the generic parameter they created it with. (In such a case you cannot put objects into the List.) This is completely different from your case here, where you are creating the List object, so you decide and therefore know exactly what generic parameter is used.

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

Sidebar

Related Questions

I'd like to be able to specify that an object's member variables are immutable
Is there any way that you can specify in Fluent NHibernate to leave out
With Spring MVC, you can specify that a particular URL will handled by a
I understand that I can specify system properties to Tomcat by passing arguments with
In code one can specify globally accessible constants/enums/etc once that can then be reused
Is it possible to specify that members of a nested class can be accessed
Is there any way I can specify a standard or custom numeric format string
How can I specify the bounds of an unapplied higher order type parameter, such
Can I specify the Jackson ObjectMapper that Spring's RestTemplate uses? I'm not 100% that's
I want to make some functions that are re-usable by any model so that

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.