Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 518
user3799927 Avatar asked Mar 15 '26 12:03

user3799927


1 Answers

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.

like image 54
Robby Cornelissen Avatar answered Mar 18 '26 01:03

Robby Cornelissen



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!