public class MyList<E> {
}
In the example above, how could I ensure that E is of a certain class when a "MyList" object is created?
public class MyList<E> {
}
In the example above, how could I ensure that E is of a certain class when a "MyList" object is created?
When you create a MyList
object for say integers, you do this:
MyList<Integer> myList = new MyList<>();
I see in your comments, you wanted to create it for type GameObject
, you can do it the same way:
MyList<GameObject> myList = new MyList<>();
You can try by using generic bound like below.
class MyList<E extends YourClass> {
}
For example :
class MyList<E extends Number> {
}
For above example, MyList only allow passing Number
or its subtype (Integer, Double etc
)
So if you try to create object like below.
MyList<Integer> list = new MyList<>(); // This will works fine as Integer is subclass of Number.
MyList<String> list = new MyList<>(); // This will give you compilation error as String is not a subclass of number.