0
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?

JackHipson300
  • 75
  • 1
  • 9
  • `E extends MyClass` – Darren Forsythe Feb 10 '18 at 19:56
  • 3
    Why make it generic if you only accept one type? – Mat Feb 10 '18 at 19:56
  • you need to restrict `E` to the class/interface you want to bound it to, like `E extends B` – Emerson Cod Feb 10 '18 at 19:56
  • I support what @Mat is suggesting. If you know that `E` is of a certain type, then the class is no longer generic... – smac89 Feb 10 '18 at 19:57
  • @Mat I am making a game and want to create a list that will only be able to hold GameObjects and I don't want to use an ArrayList because I need to have methods in it that are specific to GameObject. – JackHipson300 Feb 10 '18 at 19:59
  • @JackHipson300 why don't you use an `ArrayList` then? Or use one internally in your `MyList` class, then `MyList` doesn't need to be generic. – Kayaman Feb 10 '18 at 20:00
  • What do you mean by "ensure that E is of a certain class"? Should `E` *always* be of one specified class? If yes then what is the point of having generic type here? This looks like [X/Y problem](https://meta.stackexchange.com/q/66377). Show us your real code. – Pshemo Feb 10 '18 at 20:00
  • `public class MyList extends ArrayList { ... }`. – luk2302 Feb 10 '18 at 20:00

2 Answers2

1

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<>();
smac89
  • 39,374
  • 15
  • 132
  • 179
  • 1
    I don't think that this is what OP is asking about. – Pshemo Feb 10 '18 at 20:00
  • @Pshemo, what makes you say that? OP is asking about creating a `MyList` object for a given type. That's what this answers – smac89 Feb 10 '18 at 20:01
  • Because that still allows you to set different types for `MyList` like `MyList` `MyList` and so on. It is not limited to one specific type (not ensuring that it will be used with only one specific type). But it is true that question is not very clear so I may be misunderstanding OP. – Pshemo Feb 10 '18 at 20:03
  • @Pshemo, this is true. I was initially thinking the question was about the generic nature of the class, but I see the OP was asking about creating and instance of this class, which is what lead me to believe that this is what he was asking about – smac89 Feb 10 '18 at 20:05
1

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.
Amit Bera
  • 7,075
  • 1
  • 19
  • 42