5

Can you specify super and extends for generics in Java?

For example, let's say you have this class structure: class inheritance structure

But only want to select classes that: extends A and super ABB. Is that possible?

Marcono1234
  • 5,856
  • 1
  • 25
  • 43
  • 1
    Why do you want to prevent sub-classes of ABB? – Peter Lawrey Apr 08 '16 at 19:08
  • 1
    @PeterLawrey I want to prevent sub-classes of A – Marcono1234 Apr 08 '16 at 19:12
  • @Marcono1234 Why do you want to prevent subclasses of A? (You can do this by making A `final`) – Darth Android Apr 08 '16 at 19:18
  • 1
    @DarthAndroid I rather want for example an ArrayList to contain only objects that `extends A` and `super AAB`, the class `ABB` can still have subclasses, however they should be not allowed for the ArrayList – Marcono1234 Apr 08 '16 at 19:23
  • Isn't `AB ` a subclass of `A`? – Peter Lawrey Apr 08 '16 at 19:40
  • @PeterLawrey yes it is, but `super ABB` means that I want subclasses of `ABB` doesn't it? Or why are you asking if I want to prevent subclasses of ABB? – Marcono1234 Apr 08 '16 at 19:43
  • You could create a dummy interface that you use for the classes you want to have in the arraylist and just have the classes you want implement the dummy interface...http://stackoverflow.com/a/745769/4028085 – brso05 Apr 08 '16 at 19:47
  • `ArrayList extends mydummyinterface>` Use the interface as an identifier to identify what classes your object can `extend`...`AB implements mydummyinterface`. – brso05 Apr 08 '16 at 19:53
  • 1
    `super T` means `T` or a super class. not sub-classes. – Peter Lawrey Apr 08 '16 at 19:56
  • @PeterLawrey yes sorry I mixed it up in that comment but I meant what you said. Because then I don't understand your question "Why do you want to prevent sub-classes of ABB? " – Marcono1234 Apr 08 '16 at 21:30
  • @Marcono1234 You have stated that you want `super ABB` which means you don't want sub classes of ABB. – Peter Lawrey Apr 09 '16 at 08:38
  • @PeterLawrey, yes you are right. Sorry for the confusion I mixed it up there again. I don't necessarily want to prevent subclasses of ABB but instead only want to make sure that only this inheritance line of classes is allowed, but not for example objects of the class AA. – Marcono1234 Apr 10 '16 at 14:31
  • In this case you just need `extends ABB` – Peter Lawrey Apr 10 '16 at 14:38
  • @PeterLawrey but I want to allow classes that `extends A`. I just want the line from A to ABB – Marcono1234 Apr 10 '16 at 15:09
  • Which means you do want to exclude subclasses of ABB or is you want to exclude AA and ABA – Peter Lawrey Apr 10 '16 at 18:16
  • @PeterLawrey The latter one – Marcono1234 Apr 11 '16 at 14:50

1 Answers1

3

No, Java does not let you simultaneously specify both bounds. That said, I've never run across a situation where both bounds were needed. There's almost no reason to include some subclasses but not others - if you need to exclude subclasses, those subclasses are probably violating their contract.

Darth Android
  • 3,437
  • 18
  • 19
  • Try to add, to a restricted collection, a new one restricted element (you need both: super and extends restrictions). – josejuan Mar 07 '19 at 18:57