1

So I started learning Java about a month ago. And there is this one thing that I can't quite put my finger on.

What is difference between

List<String> list1 = new ArrayList<>();

and

ArrayList<String> list2 = new ArrayList<>();

I get that in list1 declaration is done by using name of Array class and initialization by using name of ArrayList class, and in list2 declaration and intialization both are done by using name of ArrayList class. But what is the difference if there are any.

And even when I make object for inherited class and do the same thing, declaration using name of one class and initializing using name of other. Is there any difference apart form the fact that the code looks different??

Ps : Kinda new to all the terminology and learned most stuff from books, oracle website and stuff but not completely from one source so if i made any mistake please correct me :)

1 Answers1

0

ArrayList is contained under the List interface. When you use ArrayList, you are adding more specification and using ArrayList methods.

See Polymorphism

Grayson Felt
  • 97
  • 1
  • 13
  • The answer is quite unclear. A reader may think that s*he can use all methods on a `List<...>`-variable if it references an `ArrayList<...>`. – Turing85 Sep 12 '20 at 18:29