-4
String [] s = {"A","Z","B"};
List l  = Arrays.asList(s);
System.out.println(l);

I think it is not possible to create to create object of an interface. So what is the meaning of the line

List l = Arrays.asList(s);

List is an interface so how we can create object of List?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 1
    List is the type for the reference variable and actual object is of a Concrete class implementing the List interface, this object is returned by the `Arrays.asList` method. – nits.kk May 11 '20 at 11:09
  • 4
    What do you think happens when you call `List l = new ArrayList()`? Are you creating List "interface object"? Now what if I create method like `ArrayList getList(){ return new ArrayList();}`, can we use it like `List l = getList();`? – Pshemo May 11 '20 at 11:10
  • BTW you may also be interested in [What does it mean to “program to an interface”?](https://stackoverflow.com/q/383947). And don't EVER name your variable `l` (lower `L`) since in some fonts it is similar to `1` (one) which may cause unnecessary readability problems. – Pshemo May 11 '20 at 11:21
  • Also this may be related: [Difference between Arrays.asList(array) and new ArrayList(Arrays.asList(array))](https://stackoverflow.com/q/16748030) – Pshemo May 11 '20 at 11:24

2 Answers2

0

The Arrays.asList method actually returns a new ArrayList which is an implementation of List.

Jeppz
  • 912
  • 1
  • 8
  • 31
  • 6
    It may be worth mentioning that returned ArrayList isn't `java.util.ArrayList` but inner class inside `Arrays` which is also named `ArrayList`. – Pshemo May 11 '20 at 11:11
-1

You are creating a ArrayList, which is of type List.

Asce4s
  • 819
  • 3
  • 10
  • 16
  • add details to the answer which adds more value , the current one you can put as a comment rather than posting it as an answer – nits.kk May 11 '20 at 11:11