5 ms·
The example you provide has little to do with generics, but how inheritance works. In particular if you override a method in a derived class, you're allowed to
by mrighele 4y ago
The example you provide has little to do with generics, but how inheritance works.
In particular if you override a method in a derived class, you're allowed to return an instance of a more specific class (in your example, Bar is an Object so it is fine). At runtime, your example is equivalent to something like
interface Supplier {
Object get();
}
class Foo extends Supplier {
Bar get() { return new Bar(); }
}