Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is (the type of) String.class a subtype of (the type of) Object.class?

In Java, do class objects have the same heritance relations as the classes they represent?

like image 910
Hanno Fietz Avatar asked May 22 '09 11:05

Hanno Fietz


1 Answers

The .class property always returns a Class object. The Class class (weird) has a generic parameter. The generic parameter types are subclasses.

Class<String> stringClass = String.class;
Class<Object> objectClass = Object.class;

And because with generics, Type<foo> is NOT a supertype of Type<subtype_of_foo> (see the Java tutorials), this means, that the answer is "No".

like image 73
Adam Paynter Avatar answered Oct 26 '22 14:10

Adam Paynter