There is a method that should take one of two classes:

private <T extends First | Second> Ret method(T obj); //хотел но не работает 

The problem is that both First and Second are classes that do not have a common parent. This is the wsdl generated data "top". Now I have two identical methods:

 private Ret method(First obj) private Ret method(Second obj) 

with the same body.

I want to solve the problem with a generic.

  • Can you clarify what the method does? Does he refer to some methods common to First and Second ? - default locale
  • @default locale and the modes for First and Second are the same, well, this is just a data transfusion. Why two of them do not know. I did not compose this. - Pavel
  • stackoverflow.com/q/23769208/4928642 is an interesting idea :) - Qwertiy
  • stackoverflow.com/q/5196941/4928642 - that’s something like that :) - Qwertiy

3 answers 3

OR for generalizations does not work, unfortunately. The only thing that comes to mind in such a situation is to declare a private method in the same class that accepts T extends Object , transfer logic to it and call it from method(First obj) and method(First obj) .

    As already answered, generic methods in Java do not support this kind of restriction.

    If it is possible to change the code of First and Second , then you can take out the methods that method refers to in the common interface / parent class and write one method that accepts this interface.

    If this is not possible, then you can try to wrap First and Second into classes that will implement the interface with common methods.

    Primitive example for one common method:

     class First { //общий метод void save() {...} } class Second { void save() {...} } //создаем интерфейс interface Saveable { void save(); } //класс-обертка для First class FirstSaveableWrapper implements Saveable { private final First first; FirstSaveableWrapper(First first) { this.first = first; } @Overrides public void save() { first.save(); } } //аналогичный класс для Second class SecondSaveableWrapper implements Saveable { ... } //общий метод Ret method(Saveable obj) { ... obj.save(); } //перегруженные методы для First и Second Ret method(First first) { return method(new FirstSaveableWrapper(first)); } Ret method(Second second) { return method(new SecondSaveableWrapper(second)); } 

    So we will reduce the scale of duplication of the code before calling individual methods. How justified such a struggle with duplication depends on how many First and Second common methods and what is the probability that the logic method will change for both classes / for each class separately.

    Depending on the situation, you can replace the explicitly declared wrapper classes with anonymous classes that implement the interface:

     Ret method(First first) { return method(new Saveable(first){ void save() { first.save(); } }); } 

    Or, put each method into a separate interface and use lambda expressions / method references.

      And this is not the case: we make two heirs with a common interface and use instead of the original classes of heirs: https://ideone.com/RLYP61

       import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Ideone { static class First { public void doSmth() { System.out.println("First"); } } static class Second { public void doSmth() { System.out.println("Second"); } } static interface DoSmth { void doSmth(); } static class FirstWrapper extends First implements DoSmth {} static class SecondWrapper extends Second implements DoSmth {} public static void main(String[] args) throws java.lang.Exception { doSmth(new FirstWrapper()); doSmth(new SecondWrapper()); } public static void doSmth(DoSmth smth) { smth.doSmth(); } } 
      • Great option, +1! But it is also necessary for the wrappers to accept the original objects in the constructor and delegate method calls to them, since they probably have some state. - default locale
      • @defaultlocale, so at me he inherits them - to accept in the designer and already described in the next answer. - Qwertiy