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

1

6

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?

Bunns answered 29/10, 2013 at 13:38 Comment(2)
Do they mean sub/super classes to the original type?Tetrachloride
May be they mean the difference between (int a ,String b) and (String b, int a)Kilian
O
2

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

Obolus answered 29/10, 2013 at 13:58 Comment(1)
IMO the most legit answer. docs.oracle.com/javase/tutorial/java/generics/rawTypes.htmlGrimona

© 2022 - 2024 — McMap. All rights reserved.