0

I have a method in Java that takes in a generic type:

public <K> void doSomething(List<K> inputList, Function<DB, Map<String, K>> func);

K is only allowed to to extend either one class or another (A or B)

How can I define that in Java?

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
Arian
  • 7,397
  • 21
  • 89
  • 177
  • 2
    Make an interface that does nothing....and have both A and B implement it.... then extend K on that empty interface – RobOhRob Oct 21 '19 at 18:10
  • I'm not allowed to change `A` and `B` as they are import legacy classes deeply integrated to the system. – Arian Oct 21 '19 at 19:04
  • then your best bet is to create two different `doSomething` methods... one for A... and one for B – RobOhRob Oct 21 '19 at 19:06

1 Answers1

0

I guess, you can't. You can extend only one class (should be first), and interface. Please take a look on answer: Java Generics Wildcarding With Multiple Classes