Usually declaration type is interface type and initialization part has implementation type. what is differnce between List<String> list = new ArrayList<String>
and ArrayList<String> list = new ArrayList<String>
? What is difference beside polymorphism?
Asked
Active
Viewed 639 times
-1

Tom
- 16,842
- 17
- 45
- 54

Roshan Bade
- 55
- 7
-
2Hint: it took me less than 10 seconds to find this answer. Next time, please **you** try to do that prior research. – GhostCat Dec 23 '16 at 09:32
2 Answers
1
There are no important differences. But if you use something like this
void doSomething(List list)
{}
you can use any object which extends the List class, but if you use something like this void doSomething(ArrayList list) {} you can use only ArrayList object and its subclasses.

Ihor Dobrovolskyi
- 1,241
- 9
- 19
-
Thanks, is there any easyness in terms of code edit in future if i use Interface(eg: List
list = new ArrayList – Roshan Bade Dec 25 '16 at 15:21(); -
-
-
Now, your list may be object of the ArrayList class, but lately it may be object of the LinkedList class. Read about [Polymorphism](https://www.tutorialspoint.com/java/java_polymorphism.htm) to understand it better. – Ihor Dobrovolskyi Dec 25 '16 at 15:40
0
List is an interface and ArrayList is its implimentation class. We can't create object of interface because they are abstract but we can create reference of interface which is nothing but List list . Using this reference we can invoke methods of ArrayList.

Chetana Zambare
- 9
- 1
-
This doesn't answer the question. It doesn't answer the differences between using `List<..>` or `ArrayList<..>` as variable type. – Tom Dec 23 '16 at 09:41