2

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.

Community
  • 1
  • 1
JayCD
  • 57
  • 8

1 Answers1

3

public class EntityList extends ArrayList<EntityInterface> {

Aram Kocharyan
  • 20,165
  • 11
  • 81
  • 96
  • Wont that just have EntityInterface as the name if the generic type? . for instants how is this different from . "public class EntityList extends ArrayList {" – JayCD Mar 29 '13 at 11:18
  • EntityList is no longer a generically typed class, so you would use it like: `EntityList list = new EntityList();`. – Aram Kocharyan Mar 29 '13 at 11:19
  • This looks good. Keep in mind what when you get(x) from an EntityList, you will get an EntityInterface, rather than the class of the object you actually put in. – CapnSmack Mar 29 '13 at 11:21
  • A bit confusing. From what i could tell EntityInterface in ArrayList was being used as a name. However it works. Thank you and i will make sure add rep to your answers good sir. Glad to know i'm not the only person that has trouble sleeping. – JayCD Mar 29 '13 at 11:24
  • Okay so CapnSmack has a point i didn't even think about. I'm trying to use this as a way to sort entities in a game. can i cast the objects when i use get(x) ? – JayCD Mar 29 '13 at 11:25