CharSequence[] items = { “Google”, “Apple”, “Microsoft” };
if CharSequence is an interface then, in the above example aren't we instantiating a interface?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With