Is Collection<?>
a subtype of Object
in Java? This might开发者_如何学编程 be a stupid question, but isn't Object
the root of every class?
No. Collection
is an interface, and interfaces cannot inherit from classes -- therefore they cannot inherit from Object
. (It does not make sense for an interface, which has no implementation, to inherit from a class, which can have an implementation.)
However, any class that implements Collection
will obviously have to inherit from Object
. So you can treat objects implementing Collection
as though they do inherit from Object
, because ultimately they will have to.
It's a semantic difference, but an important distinction in OO theory.
UPDATE: For those challenging this answer with the Java spec, I refer you to this code sample, which indicates that either the spec is not implemented correctly, or is just plain wrong.
TestInterface.java
public interface TestInterface {
public void foo();
}
Test.java
import java.lang.*;
import java.lang.reflect.*;
public class Test {
public static void main(String[] args) {
Class ti = TestInterface.class;
Class sti = ti.getSuperclass();
System.out.println("superclass is null: " + (sti == null));
for (Method m : ti.getMethods()) {
System.out.println(m.getName());
}
}
}
On my system (Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
according to java -showversion
) the output of the compiled program is:
superclass is null: true foo
So I'm sorry, but no, interfaces do not technically inherit from anything (except possibly other interfaces) and they do not inherit the methods of Object
.
Yes, Collection<?>
is a reference type and therefore a subtype of java.lang.Object
.
This is easy to check:
import java.util.Collection;
class Assign {
public Object check(Collection<?> things) {
return things;
}
}
Just as well, because it allows us to have collections of collections, for instance.
JLS 4.10.2 sayeth:
Given a type declaration for C, the direct supertypes of the parameterized type (§4.5) C are all of the following:
[...]
- The type Object, if C is an interface type with no direct superinterfaces.
Yes. Any interface type is a subtype of Object
.
I think a lot of the confusion here (by which I mean cdhowie's incorrect answer and the large number of upvotes it has received) centers around an incorrect understanding of the term "subtype". "Subtype" is a compile-time concept defined by the Java Language Specification that indicates a relationship between two types. While the subtype relationship is used various places in the JLS, a type S
being a subtype of a type T
effectively means that an instance of S is always also an instance of T. Given that, for a reference of type S
to be assigned to a reference of type T
without casting and without producing a compiler error, S
must be a subtype of T
!
Thus, the fact that the following code is legal is actually proof that the interface type Collection
is considered a subtype of the type Object
by the Java langauge, just as the JLS specifies.
Collection<?> collection = ...
Object obj = collection;
cdhowie's "proof" that his answer is correct does not actually prove anything other than that the methods Class.getSuperclass()
and Class.getMethods()
obey the contracts they specify in their documentation... this has nothing to do with Java's concept of a "type".
Class.getSuperclass()
:
If this
Class
represents either theObject
class, an interface, a primitive type, or void, then null is returned.
Class.getMethods()
:
This method returns an array of length 0 if this Class object represents a class or interface that has no public member methods, or if this Class object represents a primitive type or void.
精彩评论