declarinon of List list = new ArrayList()

why we declare
List list = new ArrayList();

    instead of

ArrayList list = new Arraylist();

what is advantage/difference of these two declaration.
Asheesh_KumarAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

WoodzCommented:
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.
0
sciuriwareCommented:
So, what is ArrayList more than a List.
Well, List is just an Interface.
ArrayList implements List, and:

    Serializable, Cloneable, Iterable, Collection, RandomAccess.

;JOOP!
0
Asheesh_KumarAuthor Commented:
Woodz:
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?
0
sciuriwareCommented:
Yes, at least .............. if the methods defined by the Interface 'List' are enough for you.

Compare:

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.

;JOOP!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Asheesh_KumarAuthor Commented:
satisfactory solution.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.