declarinon of  List  list = new ArrayList()

Posted on 2008-11-04
Last Modified: 2012-06-27
why we declare
List list = new ArrayList();

    instead of

ArrayList list = new Arraylist();

what is advantage/difference of these two declaration.
Question by:Asheesh_Kumar
    LVL 4

    Assisted Solution

    java.util.List is an interface, implemented by many different classes (ArrayList, LinkedList, Stack, Vector, etc...), so declaring the variable as type List rather than ArrayList, means that the underlying implementation of the collection can change as required, yet the core functionality (indexing, adding, removing, etc..) is still allowed.

    It is advantageous to keep data types as general as possible, so that the underlying implementation details can change (for example, for performance reasons you could decide to move to a stack implementation), with minimum changes to the processing code, that simply navigates over the list and operates on the items within.

    As far as I am aware, there are no disadvantages in this scenario for declaring the data type as List instead of ArrayList.
    LVL 24

    Assisted Solution

    So, what is ArrayList more than a List.
    Well, List is just an Interface.
    ArrayList implements List, and:

        Serializable, Cloneable, Iterable, Collection, RandomAccess.


    Author Comment

    you want to say that if we use

    List list = new ArrayList()..
    instead of ArrayList list = new ArrYList()...

    then if in future we want to assign Vector to List so we can do it like
    List list = new Vector()...
    no need to change/declare a new variable call Vector?
    LVL 24

    Accepted Solution

    Yes, at least .............. if the methods defined by the Interface 'List' are enough for you.


    class Mouse implements Feeding {}
    class Squirrel implements Feeding {}
    interface Feeding { public void eat(); }

    Now, both classes are to contain a method 'eat()'.

    Feeding fm = new Mouse();
    Feeding fs = new Squirrel();

    Squirrel has an additional method jumpFromTreeToTree();

    By fm or fs you can only call 'eat()', as Feeding 'knows nothing' about
    methods not defined in Feeding.


    Author Closing Comment

    satisfactory solution.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
    The viewer will learn how to implement Singleton Design Pattern in Java.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now