Because in your second example Optional.of()
expects an X
and new Y()
is an X
.
But, as you state, an Optional<Y>
is not an Optional<X>
, hence the first example doesn't work.
More generally a A<X>
is not a A<Y>
, no matter what X
and Y
are.