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.

1 Answer 1

3

public class EntityList extends ArrayList<EntityInterface> {

Sign up to request clarification or add additional context in comments.

5 Comments

Wont that just have EntityInterface as the name if the generic type? . for instants how is this different from . "public class EntityList extends ArrayList<e> {"
EntityList is no longer a generically typed class, so you would use it like: EntityList list = new EntityList();.
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.
A bit confusing. From what i could tell EntityInterface in ArrayList<EntityInterface> 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.
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) ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.