1

If I have an interface interface Example{}

ArrayList<Example> ExampleArrayList = new ArrayList<Example>();
Is possible
List<Example> ExampleList = new List<Example>();
Is not possible
List<Example> ExampleCast = new ArrayList<Example>();
Is possible

Why is that?

Also, I have noticed that if I do a generic for instance like

class genericExample<T extends Example>

It is also valid but I'm not sure if this compiles properly

Boris
  • 67
  • 8
  • 3
    `List` is an interface, so `new List();` is going to fail. You could, of course, create an anonymous class to make it work: `new List() { /* implementation */ };`. – Slaw Dec 12 '21 at 01:13
  • 1
    `class GenericExample` defines a class which takes a generic type that extends `Example`. Regarding the creation of lists, as Slaw mentioned above, the first line doesn't work because you can't create an instance of List because it is an interface. You need to instantiate an implementation of it (e.g. ArrayList) – Ben Anderson Dec 12 '21 at 01:16

0 Answers0