Search code examples
javainterfacecharsequence

Instantiating a interface CharSequence


CharSequence[] items = { “Google”, “Apple”, “Microsoft” };

if CharSequence is an interface then, in the above example aren't we instantiating a interface?


Solution

  • You're instantiating a String array and then assigning it to variable that has a CharSequence array type. This works because String is assignable to (implements) CharSequence. Here's a couple more examples:

    CharSequence cs = "Whatever";
    List<Integer> list = new ArrayList<>();
    

    So you're actually instantiating concrete types and assigning them to a variable/field that has an interface type.