• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic