Question

I'm sorry but i'm not sure if i used or am using the correct terminology, but basically i want to create a class called EntityList that extends ArrayList but i want it so that the interface I created called EntityInterface is basically being used for E in all instants of EntityList so i don't have manually declare.

code wise i want to be able to put this in

 EntityList entityList = new EntityList();

and get the equivalent result of this

 EntityList<EntityInterface> entityList = new EntityList<EntityInterface>();

I'm really unsure how to proceed from here. Here's was my attempt at the problem.

 import java.util.ArrayList;

//right now just a class that extends ArrayList designed to hold Entities.
public class EntityList<EntityInterface> extends ArrayList<EntityInterface> {

    EntityList(){

    }
}

The best way i can show the problem is to show as if the arraylist is a instants variable.

 import java.util.ArrayList;

//right now just a class that extends ArrayList designed to hold Entities.
public class EntityList<EntityInterface>  {

    ArrayList al = new ArrayList();

    EntityList(){

    }
}

However this is basically what i want, again in variable form and not extends form.

 import java.util.ArrayList;

//right now just a class that extends ArrayList designed to hold Entities.
public class EntityList<EntityInterface>  {

    ArrayList<EntityInterface> al = new ArrayList<EntityInterface>();

    EntityList(){

    }
}

I believe if i do something like this,

 public class EntityList<? implements EntityInterface> extends ArrayList<?>  {

it should work based on this thread. Generic: ArrayList of ? Extends ISomeInterface in Java

however after trying it on eclipse with this modification of my code :

public class EntityList<E implements EntityInterface> extends ArrayList<E> {

and public class EntityList extends ArrayList { both were throwing errors in eclipse. I am using Java 6. I'm not sure of what sub version i'm using. I do have Java 7 if that will correct the error i'll try it.

Was it helpful?

Solution

public class EntityList extends ArrayList<EntityInterface> {

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top