File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes new Class<? extends SuperClass> { Subclass1.class, Subclass2.class}; ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "new Class<? extends SuperClass> { Subclass1.class, Subclass2.class}; ??" Watch "new Class<? extends SuperClass> { Subclass1.class, Subclass2.class}; ??" New topic
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
    
  19

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)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: new Class<? extends SuperClass> { Subclass1.class, Subclass2.class}; ??
 
Similar Threads
Method Overloading
Confused about overloading and overriding
Doubt in assigning super class and subclass variables
A tricky Question
protected constructors