0

I have a

public abstract class Person{/*..*/}

public class Woman extends Person{/*..*/}
public class Man extends Person{/*..*/}

I'm trying to instantiate a List that could contain either Man or Woman, I tried this :

List<? extends Person> PersonTypes = new List<? extends Person>()

But it can't be instantiated, is there a way to achieve what I want and keeping Person abstract ?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
user3332598
  • 95
  • 1
  • 12

3 Answers3

2

It should work.

List<Person> PersonTypes = new ArrayList<Person>()
RMachnik
  • 3,598
  • 1
  • 34
  • 51
2

List is an Interface and thus cannot be instantiated itself, similar to your abstract Person class which cannot be instantiated either.

You should instantiate a class which implements List, such as ArrayList:

List<Person> people = new ArrayList<Person>();
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
Daniël Knippers
  • 3,049
  • 1
  • 11
  • 17
0

The List is also an interface and cannot be instantiated. Try this:

List<? extends Person> PersonTypes = new ArrayList<Person>();
jgitter
  • 3,396
  • 1
  • 19
  • 26