| Author |
new Class<? extends SuperClass> { Subclass1.class, Subclass2.class}; ??
|
B Atkins
Greenhorn
Joined: Jun 11, 2009
Posts: 11
|
|
Ok, my problem is creating an array of Classes, such that the classes are all subclasses of a specific super class.
This should be possible, I believe, since the objects being instantiated are of type "Class", and not instances of the named classes. Doesn't the compiler have enough information to create this, and enforce it?
Furthermore, couldn't SuperClass also be abstract, or an interface, for the same reasons?
I realize the compiler may reject this, but does it really have to? Could it allow this type of initialization in the specific case of Class generics?
Thanks!
Batkins
|
Two wrongs don't make a right... but three lefts do.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16690
|
|
I realize the compiler may reject this, but does it really have to? Could it allow this type of initialization in the specific case of Class generics?
Yes, but what would the use of allowing this type of initialization? It is unable to guarantee to type check it later when objects are assigned to array members -- so you aren't going to get the type safety regardless. So... what wrong with just an array of Class, and type check it yourself during initialization? You have to do it everywhere else anyway.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
 |
|
|
subject: new Class<? extends SuperClass> { Subclass1.class, Subclass2.class}; ??
|
|
|