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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:21:04+00:00 2026-05-31T23:21:04+00:00

Possible Duplicate: Why should the interface for a Java class be prefered? When should

  • 0

Possible Duplicate:
Why should the interface for a Java class be prefered?

When should I use

List<Object> list = new ArrayList<Object>();

ArrayList inherits from List, so if some features in ArrayList aren’t in List, then I will have lost some of the features of ArrayList, right? And the compiler will notice an error when trying to access these methods?

  • 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-31T23:21:06+00:00Added an answer on May 31, 2026 at 11:21 pm

    The main reason you’d do this is to decouple your code from a specific implementation of the interface. When you write your code like this:

    List list = new ArrayList();  
    

    the rest of your code only knows that data is of type List, which is preferable because it allows you to switch between different implementations of the List interface with ease.

    For instance, say you were writing a fairly large 3rd party library, and say that you decided to implement the core of your library with a LinkedList. If your library relies heavily on accessing elements in these lists, then eventually you’ll find that you’ve made a poor design decision; you’ll realize that you should have used an ArrayList (which gives O(1) access time) instead of a LinkedList (which gives O(n) access time). Assuming you have been programming to an interface, making such a change is easy. You would simply change the instance of List from,

    List list = new LinkedList();
    

    to

    List list = new ArrayList();  
    

    and you know that this will work because you have written your code to follow the contract provided by the List interface.

    On the other hand, if you had implemented the core of your library using LinkedList list = new LinkedList(), making such a change wouldn’t be as easy, as there is no guarantee that the rest of your code doesn’t make use of methods specific to the LinkedList class.

    All in all, the choice is simply a matter of design… but this kind of design is very important (especially when working on large projects), as it will allow you to make implementation-specific changes later without breaking existing code.

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

Sidebar

Related Questions

Possible Duplicate: Why should the interface for a Java class be prefered? ArrayList<Integer> al
Possible Duplicate: When should one use final? When should Java programmers prefer to use
Possible Duplicate: Should I use "from package import utils, settings" or "from . import
Possible Duplicate: Should C++ eliminate header files? In languages like C# and Java there
Possible Duplicate: Why should I use templating system in PHP? I was just curious
Possible Duplicate: How many Python classes should I put in one file? Coming from
Possible Duplicate: Should I learn C before learning C++? As a professional (Java) programmer
Possible Duplicate: Should I use uint in C# for values that can’t be negative?
Possible Duplicate: Should I use Elements or Attributes in XML? I'm writing a configuration
Possible Duplicate: When should you use 'friend' in C++? I was brushing up on

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.