error in overriding generic collections in Java

前端 未结 5 1825
隐瞒了意图╮
隐瞒了意图╮ 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:54

    To add to the answers already here, I want to comment about the signature of the methods is the same after erasure... but the compiler checks the method type before erasure.

    You could do something like creating a "different" Parent class, such as

    class Parent {
      public void getname(List num) {
        System.out.printf("child class: %s",num);
      }
    }
    

    , compile it, use it to compile Child class, and then mix your original Parent.class and Child.class in the same JVM without issue, avoiding the compiler checks and using type erasure.

    But as long as the compiler notices doing something like that "in the same run", it will fail by the reasons explained by Ashot and Louis.

提交回复
热议问题