0

I have created generic interface.

public interface Abc<T> {
    void validatePojo(T input);
}

Following two class are implemation of above interface.

1)-----------------------------------------------
  public class Hello implements Abc<Pojo1> {
       @Override
       public void validatePojo(Pojo1 input) {
          // some code
       }
  }
2)-----------------------------------------------
  public class Hi implements Abc<Pojo2> {
       @Override
       public void validatePojo(Pojo2 input) {
          // some code
       }
   }

Now when I tried to create object of Abc,

T input = getInput(someInput);    // getInput return either Pojo1 or Pojo2
Abc abc = someFactory(someInput); //someFactory(someInput) will return either `new Hello()` 
  ^                               //or `new Hi()` based on `someInput`
  |
  +-------------------------------//warning
abc.validate(input);

public Abc<?> someFactory(final int input) {
    return input == 1 ? new Hi() : new Hello();
}

public T getInput(final int input) {
    return input == 1 ? new Pojo1() : new Pojo2();
}

I am getting waring Abc is a raw type. References to generic type Abc<T> should be parameterized.

How can I reslove this warning?

I looked on the internet found following but it is not very useful.

  1. One way that I found was to use @SuppressWarnings.
  2. Declare variable like Abc<Pojo1> abc or Abc<Pojo2> abc, I can not do this as whehther to use Pojo1 or Pojo2 is totally depend on input.(I don't want to write logic of factory method here)

Is there any other way to reslove it?

yajiv
  • 2,901
  • 2
  • 15
  • 25

1 Answers1

0

The compiler is just imforms you, that Abc is generic and is dependant on the type you are passing in your Factory. Therefore:

Abc<?> abc = ...
Abc<? extends Object> abc = ...

as mentioned in the comments should be valid. The extend version is useful, if you want to specify which type of object you pass.

Lecagy
  • 489
  • 2
  • 10
  • If I use Abc> then I am getting error for abc.validatePojo(input). saying The method validatePojo(capture#2-of ?) in the type Abc is not applicable for the arguments (T). Here assume that input is either Pojo1 or Pojo2. – yajiv Oct 07 '19 at 09:10