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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:57:39+00:00 2026-05-16T08:57:39+00:00

I see this syntax a lot and don’t understand the reasoning behind it. I

  • 0

I see this syntax a lot and don’t understand the reasoning behind it. I thought you generally want to work with classes rather than interfaces to make it easier to carry out the full panoply of operations you might want to perform.

Why do this:

List<Foo> foo = new ArrayList<Foo>(something.getFoo());

instead of this:

ArrayList<Foo> foo = new ArrayList<Foo>(something.getFoo());

when you later want to perform operations on foo? Isn’t it easier if foo is represented by the class ArrayList rather than the interface List?

  • 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-16T08:57:39+00:00Added an answer on May 16, 2026 at 8:57 am

    Occasionally, yes. If you actually need a method that’s declared in ArrayList<T> but not in List<T> then sure, go for it.

    However, the reverse is more flexible – if you don’t need any methods declared in ArrayList<T>, then your code can express that by declaring the variable to be of type List<T> instead. That says, “I just need a list. I happen to be picking ArrayList<T>, but just because its behaviour suits me… not because I need any extra features it exposes.”

    That’s useful to know if later someone wonders whether they could substitute a different implementation, for example. It also limits how much a reader has to think about… the more general the type of the variable, the less one can do with it – so the less one needs to think about it.

    For example, if I see a variable of type Iterable<T> I know there’s precious little that’s likely to be done with it later: basically, it’s going to be used to iterate, and that’s all. I don’t need to worry about whether any later code is going to try to add a value to the collection, or access it by index etc.

    This is relatively unimportant for local variables, but much more so for instance variables and particularly parameters: the less concrete you make your parameters, the more flexibility the caller has about which arguments they’ll pass.

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

Sidebar

Related Questions

I don't understand why some people use the percentage syntax a lot in ruby.
See also SO 1664677 I get this error 2: Syntax error: ( unexpected when
I occasionally see the list slice syntax used in Python code like this: newList
I see this from time to time and want to know what it is.
I see a lot of Objective-C code that has the following syntax when attempting
Looking at the source code of pstats I see this syntax: print >> self.stream,
I feel dumb for not knowing this, but I see a lot of links
see this demo from jquery ui you have to hold down the Ctrl key
See this code: var jsonString = '{id:714341252076979033,type:FUZZY}'; var jsonParsed = JSON.parse(jsonString); console.log(jsonString, jsonParsed); When
See this Image below http://i46.tinypic.com/2pt6jkn.jpg This is report in SSRS as shown when it

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.