I am using a method that returns type List<>
, and I am then able to call methods on that List
:
List<Artist> artists = artistsPager.artists.items;
artists.add(new Artist());
To be clear artistsPager.artists.items
is returning type List<Artist>
.
Since List
is only an interface, how is the compiler letting me use this artists
object, doesn't a List<>
always need an implementation, like ArrayList
?
For example, the compiler won't let me do the following, because "List is abstract; cannot be instantiated":
List<Integer> list = new List<Integer>();
How is artistsPager.artists.items
any different than new List<Integer>()
?
For those curious, this come from the Java Web API wrapper for Android. All help is greatly appreciated.
I do not believe this is a duplicate of this question because even though the answers are the same, the question that begets them is different. The other question is asking the difference between type List
and ArrayList
, whereas my question was wondering why the compiler was not complaining when the implementation of List
was hidden.
I am fine with having this marked as a duplicate to direct future users to that question, but I do not think it should be closed as the answers here I think would be immensely helpful to other users.
artistsPager.artists.items
is a field of typeList<>
. Somewhere in the code, it must have been instantiated with a concrete implementation likeArrayList
.new List<Integer>()
is a constructor call, which is invalid becauseList
is an interface.