1

I know that an object cannot be created from an interface like this :

List list2 = new List();  // error.

When i work with Arrays.asList(),i'm confused, because this function returns List and the following code works perfectly:

List list1 = Arrays.asList("a","b","c");  // works perfectly

Right side of this equation returns List. Then the code becomes List list1=new List(); How can this be possible and how this code works although the right side returns an interface, i didn't understand. Can you explain it please? Thanks in advance

metis
  • 1,024
  • 2
  • 10
  • 26
  • _Then the code becomes List list1=new List();_ No. – Sotirios Delimanolis Jul 22 '15 at 17:50
  • You shouldn't see it so mathematically, there's more to it. And no, it's not the interface that gets instantiated. Read more about `polymorphism` here: https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html – mystarrocks Jul 22 '15 at 17:50
  • You asked a question a while ago where the accepted answers also covers your confusion: [here](http://stackoverflow.com/questions/30968453/outputstream-class-is-used-for-writing-into-files-how-is-it-possible) – Sotirios Delimanolis Jul 22 '15 at 18:10

3 Answers3

4

Arrays.asList(...) returns a new ArrayList.

Due to polymorphism it can state that it returns a List, and then return anything that implements the List interface. An ArrayList is a list, so it can be returned.

Extra Note: The ArrayList that asList is using is actually created in the Arrays class as an anonymous class that extends the functionality of the ArrayList class to create a constructor that accepts a native array.

Connorelsea
  • 2,308
  • 6
  • 26
  • 47
  • In the documentation, http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#asList(T...) , there is no saying about returning an arraylist , so you are wrong about "Arrays.asList(...) returns a new ArrayList." i think. Document does not say anything about returning an arraylist – metis Jul 22 '15 at 17:58
  • 1
    Documentation doesn't necessarily give such great details about the internal implementation. To find out the true story, which reveals that it DOES return an ArrayList, check the actual code. http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/Arrays.java#Arrays.asList%28java.lang.Object%5B%5D%29 – Connorelsea Jul 22 '15 at 18:03
  • Is it possible to look at the implementation via Eclipse? I use ctrl space and click the method. But a nonsense explanationn window is appeared – metis Jul 22 '15 at 18:07
  • Yeah it should be. I think by holding control and clicking a certain class. Is the answer I provided correct in the original answer? if so, then mark it as such. Also check here: http://stackoverflow.com/questions/426084/how-do-i-view-jres-source-code-in-eclipse – Connorelsea Jul 22 '15 at 18:10
1

List is an "Interface". You cannot create a new instance ( via new ...) from an Interface, you need a "real" class for that. Arrays.asList is a method that returns an object.

For your first code, you must do something like...

List list2 = new ArrayList();

ArrayList is a Class that implements the List interface. But sorry, I fear that you need to consult some basic Java and OOP tutorials to understand what classes, interfaces, objects, etc. really are, as this is not the place for something like this.

Florian Schaetz
  • 10,454
  • 5
  • 32
  • 58
  • 1
    I know your explanations. However my confusion is different. How can an object be created by using a static method which returns an interface? – metis Jul 22 '15 at 18:02
  • 1
    `public static List createList() { return new ArrayList(); }` would be the most easy example. It creates an ArrayList and because `ArrayList implements List` (in other words, ArrayList IS also a List), you can return is as List, thus "hiding" the fact what class it is exactly from the outside. If you were to change your method, for example to `return new LinkedList();` nobody would cry as it would still return a List, as promised (because `LinkedList implements List`). – Florian Schaetz Jul 22 '15 at 18:33
1

The thing is that List is an interface, not a class. You can only instantiate classes, not interfaces under Java. So you either need to create a new type that implements List (e.g. ArrayList or LinkedList, i.e. List list2 = new ArrayList();) or you can obtain a list like you did by calling a method that creates a list.

joosts
  • 154
  • 1
  • 6