0

What are the differences between these two generic statements? They seem to do the same thing and it appears to not have any effect at compile time because I can use them interchangeably with no errors.

class Foo {

}

class FooBar extends Foo {

}


interface FooService {

    Set<? extends Foo> getFooObject();

    <T extends Foo> Set<T> getFooObjectAlternative();

}

class FooServiceImpl implements FooService {

    @Override
    Set<FooBar> getFooObject() {
        // works
    }

    @Override
    Set<FooBar> getFooObjectAlternative() {
        // works but you get a warning
    }

    @Override
    Set<Integer> getFooObjectAlternative() {
        // works for some reason!
    }

}
Ogen
  • 6,499
  • 7
  • 58
  • 124
  • 1
    You can write `Set f = getFooObject();` with the second version of the method but not with the first version. – Erwin Bolwidt Jul 26 '16 at 02:51
  • @ErwinBolwidt That is a pretty big difference. Thanks for pointing it out. I wonder if there are any other differences... – Ogen Jul 26 '16 at 02:54
  • Returning a `Set` should definitely be a compile-time error if the wildcard is ``. Are you sure this code is actually what you tested? – Daniel Pryden Jul 26 '16 at 03:17
  • @DanielPryden I don't have any return statements in my code snippets though – Ogen Jul 26 '16 at 03:47

0 Answers0