Cannot override a method because of a name clash

580 Views Asked by At

This code doesn't compile:

import java.util.List;

class A {
  void foo(List l) { }
}

class B extends A {
  void foo(List<?> l) { }
}

However, the following code compiles (foo in D overrides foo in C). Why?

class C {
  void foo(List<?> l) { }
}

class D extends C {
  void foo(List l) { }
}
1

There are 1 best solutions below

3
On

The second example compiles because List<> derives from List, but not the other way around which is why the first example doesn't compile.