Lower bounded wildcard not checked against upper bounded type parameter

前端 未结 4 2344
耶瑟儿~
耶瑟儿~ 2021-02-20 03:01

I wonder why does this piece of code compile successfully?

Source code:

abstract class A
{
    public abstract  A

        
4条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-20 03:30

    Adding to the first example doesn't add anything the compiler cares about. Remember you are saying "a type which is a supertype of M" if we are saying "M is a subtype of Number" and "the type is a supertype of M", we aren't actually saying if the type is a subtype of Number.

    For a better example, have M be Integer and the variable be of type A. While obviously that won't work, it satisfies all the requirements of the function correctly.

    Since there exists no way to fix the function definition, it just lets it pass and assumes the call site will catch the problem.

    提交回复
    热议问题