Type erasure, overriding and generics
Asked Answered
C

2

36

Can someone explain to me why

@Override
public void fooMethod(Class<?> c)

doesn't override

public void fooMethod(Class c)

and gives me the following errors instead:

 - Name clash: The method fooMethod(Class<?>) 
of type SubClass has the same erasure as fooMethod(Class) of 
type SuperClass but  does not override it

 - The method fooMethod(Class<?>) of type 
SubClass must override a superclass method

?

Edit: "java -version" says Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_16-b06-284). As for the code snippet, it's already above, pretty much; the above extends the one below.

Campman answered 2/2, 2009 at 9:0 Comment(3)
Where's the return type?Casualty
Although it's missing, it doesn't matter.Proa
Can't reproduce. Can you provide a the code snippet, with the jdk details.Yount
K
47

The signature of fooMethod(Class<?>) is the same as the signature of fooMethod(Class) after erasure, since the erasure of Class<?> is simply Class (JLS 4.6). Hence, fooMethod(Class) is a subsignature of the fooMethod(Class<?>) but not the opposite (JLS 8.4.2).

For overriding with instance methods you need the overriding method to be a subsignature of the overridden method (JLS 8.4.8.1). This is clearly not the case here.

Now that we have established the fact that your subclass method doesn't override the superclass method according to the JLS, let's look at the runtime implications when type erasure has occured. We now have two methods that look exactly the 'same' (same name, same parameter types) but do not override each other. If they don't override, they must be both available on the subtype as separate methods, but they have identical runtime signatures: conflict. So Java has to disallow it.

Overriding generic parameter types using raw parameter types is allowed because raw types exist just for this reason: they are a convenient mechanism with specific unsound type rules to accommodate interaction with legacy code. So the type system here will decide that the subclass method does override the superclass one, they are identical after type erasure and we can never have a conflict. As a consequence of this libraries can be generified independently of existing non-generic code.

Kurdistan answered 2/2, 2009 at 10:23 Comment(3)
This answers the question but leads to a followup question: what do you do about the warning that inevitably comes when you use class in the subclass? Class is a raw type. References to generic type Class<T> should be parameterized.Centipede
Annotate the parameter or method with @SuppressWarnings("rawtypes").Kenley
@Kurdistan just to clarify, so if he had done public void fooMethod(Class<SomethingSpecific> c) it would be fine?Thionic
R
-3

Because Class<?> is more specific than just Class.

For example, foo(Class<List>) can't override foo(Class<Collection>). I forget the term, but types with generics will always be different from those without.

Rockfish answered 2/2, 2009 at 9:4 Comment(2)
No, it's related to generic types not beeing castable. Class<List> is not a subtype of Class<Collection> - they are entirely seperate. You must use super or extends to express the relation.Rockfish
You're talking about the fact that generics are invariant in Java.Autography

© 2022 - 2024 — McMap. All rights reserved.