Java generics name clash , has the same erasure

前端 未结 6 1588
[愿得一人]
[愿得一人] 2020-12-15 03:56

I have superclass Foo. And a class Bar extending it.

public class Bar extends Foo

Function in Foo:

protected void saveAll(C         


        
6条回答
  •  不思量自难忘°
    2020-12-15 04:32

    Due to the type erasure feature of Java, the JVM will not be able to know whether it is the method that has the parametrized type MyClass or the first one that should be called.

    If possible or applicable, the most commonly used pattern I've seen to avoid this is to change the class Foo to have a parametrized type as well:

    public class Foo {
        protected void saveAll(Collection many) {}
    }
    

    and then have Bar simply implement Foo for your specific type:

    public class Bar extends Foo {
        public void saveAll(Collection many) {
            super.saveAll(many);
        }
    }
    

提交回复
热议问题