• Post Reply Bookmark Topic Watch Topic
  • New Topic

new Class<? extends SuperClass> { Subclass1.class, Subclass2.class}; ??  RSS feed

 
B Atkins
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!