Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

Class of subclass as parameter

Thread view: 
Peter Ashford - 18 Sep 2006 00:58 GMT
Hi There,

I have a class which maps a name to a class like so:

class Mapping {
   private String name;
   private Class runner;

   public Mapping(String name, Class runnerClass){
       m_name  = name;
       m_runner = runnerClass;
   }
}

What I want to be able to do is to change that so the runnerClass can
be defined as only being the Class object of a Class which extends a
specific class ("EditorRunner" in this case).

Something like this compiles, but doesn't give the required results:

class Mapping {
   private String name;
   private Class<EditorRunner> runner;

   public Mapping(String name, Class<EditorRunner> runnerClass){

       m_name  = name;
       m_runner = runnerClass;
   }
}

With this latter code, it works for EditorRunner.class as a parameter
but not for the .class member of any subclass of EditorRunner.

Anyone know how I can get the desired effect?

Cheers,

Peter.
Michael Rauscher - 18 Sep 2006 01:36 GMT
Peter Ashford schrieb:

> class Mapping {
>     private String name;
[quoted text clipped - 11 lines]
>
> Anyone know how I can get the desired effect?

Use <? extends EditorRunner>.

To be more generic (I replaced runner because to me it's
indistinguishable how a "Mapping" is related to a "runner")

class Mapping<T> {
    private String name;
    private Class<? extends T> clazz;

    public Mapping( String name, Class<? extends T> clazz ) {
        this.name = name;
        this.clazz = clazz;
    }
}

Bye
Michael
Robert Klemme - 18 Sep 2006 02:21 GMT
> Use <? extends EditorRunner>.
>
[quoted text clipped - 10 lines]
>     }
> }

I'd probably also consider to make Mapping the complete mapping (i.e.
all name - class pairs).  Depending on the OP's problem to solve that
might or might not be more useful.

Kind regards

    robert


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.