Gmacar
Gmacar

Reputation: 223

Cannot override a method because of a name clash

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) { }
}

Upvotes: 4

Views: 571

Answers (1)

Chris Gessler
Chris Gessler

Reputation: 23123

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

Upvotes: 2

Related Questions