Search code examples
javaoverridingopen-closed-principle

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?


Solution

  • 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>.