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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:20:02+00:00 2026-05-27T23:20:02+00:00

Reading other people’s code, I’ve seen a lot of: List<E> ints = new ArrayList<E>();

  • 0

Reading other people’s code, I’ve seen a lot of:

List<E> ints = new ArrayList<E>();
Map<K, V> map = new HashMap<K, V>();

My question is: what is the point/advantage of instantiating them that way as opposed to:

ArrayList<E> ints = new ArrayList<E>();
HashMap<K, V> map = new HashMap<K, V>();

What also makes it odd is that I’ve never seen anything like:

CharSequence s = new String("String");

or

OutputStream out = new PrintStream(OutputStream);

Duplicates (of the first part of the question):

When/why to use/define an interface

Use interface or type for variable definition in java?

When should I use an interface in java?

why are interfaces created instead of their implementations for every class

What's the difference between these two java variable declarations?

  • 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-27T23:20:02+00:00Added an answer on May 27, 2026 at 11:20 pm

    Quick answer? Using interfaces and superclasses increases the portability and maintainability of your code, principally by hiding implementation detail. Take the following hypothetical example:

    class Account {
        private Collection<Transaction> transactions;
    
        public Account() {
            super();
            transactions = new ArrayList<Transaction>(4);
        }
    
        public Collection<Transaction> getTransactions() {
            return transactions;
        }
    }
    

    I’ve declared a contract for an Account that states that the transactions posted to the account can be retrieved as a Collection. The callers of my code don’t have to care what kind of collection my method actually returns, and shouldn’t. And that frees me to change up the internal implementation if I need to, without impacting (aka breaking) unknown number of clients. So to wit, if I discover that I need to impose some kind of uniqueness on my transactions, I can change the implementation shown above from an ArrayList to a HashSet, with no negative impact on anyone using my class.

    public Account() {
        super();
        transactions = new HashSet<Transaction>(4);
    }
    

    As far as your second question, I can say that you use the principal of portability and encapsulation wherever they make sense. There are not a terrible lot of CharSequence implementations out there, and String is by far the most used common. So you just won’t see alot of developers declaring CharSequence variables in their code.

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

Sidebar

Related Questions

I have seen some very weird for loops when reading other people's code. I
I'm a first time Objective C programmer. I've been reading other people's code and
While learning c#, I have picked up the implication from reading other people's code
When learning new languages such as C++ from PHP, does reading other language snippets
I have seen this similar question. (I highlighted the important bits to make skim-reading
I've been reading the other questions dealing with master pages, but I didn't see
Reading MSDN (and other sources) about custom report items (CRI) for reporting services 2005.
Other than reading the GetFileName() file and reading to line GetFileLineNumber() of a stackframe
After reading a bit more about how Gnutella and other P2P networks function, I
After reading the Head First Design Patterns book and using a number of other

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.