error in overriding generic collections in Java

前端 未结 5 1826
隐瞒了意图╮
隐瞒了意图╮ 2021-01-28 19:29

When I try to override a method that takes a List, I get the following compile error.

Multiple markers at this

5条回答
  •  说谎
    说谎 (楼主)
    2021-01-28 19:50

    The error you have pointed out is obviously because of the @Override annotation. When validating annotations (which I presume must happen at the very early stage of compiling process) the type erasure hasn't occurred. So the types List is not same as List and you get the warning. But even without that annotation I get an error

        name clash: getname(java.util.List) in child and getname(java.util.List) in parent have the    same erasure, yet neither overrides the other
    class child extends parent{`. 
    

    This error clearly says that they have the same erasure but still neither over rides. So Java in principle doesn't allow it. I can understand it will lead to many problems/confusions if allowed. For e.g. if someone makes a call new Child().get_name( new List () ), it can't be resolved to the Child method which will break the concept of over riding. So it is correct that it is not allowed.

提交回复
热议问题