Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Arrays.asList(T... a) return an interface?

Tags:

java

arrays

oop

I am looking at the Java API from Oracle, particularly at this method that is part of the java.util.Arrays class:

public static <T> List<T> asList(T... a)

But how is it possible that this method is returning a List object when clearly it is an interface?

like image 685
zxgear Avatar asked Mar 07 '26 14:03

zxgear


1 Answers

  • It is called polymorphism
  • It means you can refer to a subtype using supertype reference
  • In this case, the method is returning instances of class that implement the List interface

Source code of Arrays.asList method

 public static <T> List<T> More ...asList(T... a) {
   return new ArrayList<T>(a);
}
like image 194
Prasad Kharkar Avatar answered Mar 10 '26 04:03

Prasad Kharkar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!