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

  • Home
  • SEARCH
  • 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 4536260
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:30:42+00:00 2026-05-21T14:30:42+00:00

If you wanted to store an array of objects of type MyInterface , are

  • 0

If you wanted to store an array of objects of type MyInterface, are the following both acceptable and if so when would you use the second form over the first?

i) Using only an interface:-

List<MyInterface> mylist = new ArrayList<MyInterface>();

ii) Using a generic wildcard:-

List<? extends MyInterface> mylist = new ArrayList<? extends MyInterface>();

Edit:

As the answers so far have pointed out, number ii won’t compile. What is the difference between i and a case iii where :-

iii) Using a generic wildcard only in the reference:-

List<? extends MyInterface> mylist = new ArrayList<MyInterface>();
  • 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-21T14:30:43+00:00Added an answer on May 21, 2026 at 2:30 pm

    Second one won’t compile. Imagine:

    A implements MyInterface
    B implements MyInterface
    

    Then the following would match your second expression, but won’t compile:

    // incorrect
    List<A> mylist = new ArrayList<B>();
    

    Correction: Wrong one too:

    List<? extends MyInterface> mylist = new ArrayList<MyInterface>();
    

    It is right in a sense it does compile, but you cannot add any subclasses of MyInterface to it. Confusing, but correct — after I read the explanation. Same reason: wildcard can be viewed for example as:

    // I know this is not compileable; this is internal compiler "thinking".
    // Read it as "somewhere someone may instantiate an ArrayList<A> and pass 
    // it down to us; but we cannot accept it as something that could be 
    // potentially used as List<B>"
    List<A> mylist = new ArrayList<MyInterface>();
    

    So this won’t work:

    mylist.add(b);
    

    and vice versa. Compiler refuses to do those potentially incorrect operations.

    The option which allows you to add any subclass of MyInterface to mylist is:

    List<MyInterface> mylist = new ArrayList<MyInterface>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using anonymous namespaces to store local data and functions and wanted
I am using scala 2.7.7, and wanted to parse CSV file and store the
I wanted to wrap a small C++ code allocating an array with ctypes and
Wanted to convert <br/> <br/> <br/> <br/> <br/> into <br/>
Wanted to get some consensus around a UI feature I'm working on right now.
I wanted some of those spiffy rounded corners for a web project that I'm
I wanted to show the users Name Address (see www.ipchicken.com ), but the only
I wanted to emulate a popular flash game, Chrontron, in C++ and needed some
I wanted to generate one fix view using interface builder, but the size of
Just wanted to get an idea for ways (web) developers get round the short

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.