Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method overriding: same argument list types (or COMPATIBLE types)?

In the book I use to prepare for the new Oracle Certified Professional - Java SE7 Programmer exam, in the section that deals with method overriding, I have come across the following:

The overriding method should have the same argument list types (or compatible types) as the base version.

What do they mean by "compatible types"? I mean, as soon as the argument list types differ, you're overloading, not overriding.

I can only think of overriding a method that takes varargs arguments, with one that takes an array of the same type. Compiler gives a warning, but compiles still.

What do they mean by compatible types? Is that an error in the book?

like image 228
Peter Perháč Avatar asked Nov 01 '22 11:11

Peter Perháč


1 Answers

Maybe it has something to do with type erasure. This is valid Java, it just gives you a warning:

abstract class Foo {
    public abstract void method(List<String> xs);
}

class Bar extends Foo {
    @Override
    public void method(List xs) {
    }
}

The raw type List is compatible with List<String>.

like image 85
Tobias Brandt Avatar answered Nov 15 '22 06:11

Tobias Brandt