0

I wrote a simple class that parametrizes an object inside it

public class FooToReturn<T extends Foo>{
    T object;
    public T getObject(){
       return object;
    }
}

and some function in another class needs to return a parametrized instance of it, like

public FooToReturn doSomething(){
   ....
   return new FooToReturn<ChildFoo>();
}

Is it possible to just keep the return FooToReturn in the declaration or will it give me error at runtime? Eclipse already gives me this warning at the function declaration.

References to generic type FooToReturn<T> should be parameterized
riciloma
  • 1,456
  • 2
  • 16
  • 31
  • 1
    Well try it!! But no it won't give you an error at runtime, because of type erasure... Basically generics in Java give you more safety at compile time than at runtime. – JP Moresmau May 27 '15 at 15:15
  • Change the signature of `doSomething` to `public FooReturn extends Foo> doSomething()` – Chetan Kinger May 27 '15 at 15:22
  • Yes! That did the trick. However, I now need to declare `protected void notify (FooToReturn extends Foo> fooToReturn){ otherObject.update(this, fooToReturn.getEvent()); }` and the `update` is overloaded various types that T can assume. It still wants Foo and not FooChild as argument. What can I do? – riciloma May 27 '15 at 15:38

0 Answers0