Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #542
| Date | 2011-04-05 15:09 -0700 |
|---|---|
| From | Patricia Shanahan <pats@acm.org> |
| Newsgroups | comp.lang.java.help |
| Subject | Re: Instantiate an abstract class |
| References | <61c7300d-6011-4639-a5f1-5cd4c58906fa@r4g2000prm.googlegroups.com> <ing2nd$627$1@dont-email.me> <edce4c55-5560-4718-b3b2-d9797dff79fc@l39g2000yqh.googlegroups.com> |
| Message-ID | <b4GdnUdhF-OlDgbQnZ2dnUVZ_gSdnZ2d@earthlink.com> (permalink) |
On 4/5/2011 2:54 PM, Rob McDonald wrote:
...
> Most of my online searches had lead down the reflection path and I
> quickly got myself wrapped around the axle. I'm going to go with the
> above implementation, but I am curious about how it could be made to
> work the other way.
...
Obviously, a real implementation would handle the exceptions for a
subclass that does not have an accessible parameterless constructor, but
this should give you an idea. The advantage of this approach is that you
only need to require each subclass to have an accessible parameterless
constructor.
public abstract class AFoo {
private AFoo getNewInstance() throws InstantiationException,
IllegalAccessException {
return (AFoo) getClass().newInstance();
}
public static void main(String[] args) throws InstantiationException,
IllegalAccessException {
AFoo bob = new Bob();
AFoo bobExtra = bob.getNewInstance();
System.out.println(bobExtra.getClass());
AFoo fred = new Fred();
AFoo fredExtra = fred.getNewInstance();
System.out.println(fredExtra.getClass());
}
}
class Bob extends AFoo {
}
class Fred extends AFoo {
}
Back to comp.lang.java.help | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Instantiate an abstract class Rob McDonald <rob.a.mcdonald@gmail.com> - 2011-04-05 14:14 -0700
Re: Instantiate an abstract class Patricia Shanahan <pats@acm.org> - 2011-04-05 14:37 -0700
Re: Instantiate an abstract class Rob McDonald <rob.a.mcdonald@gmail.com> - 2011-04-05 14:49 -0700
Re: Instantiate an abstract class Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-04-05 17:48 -0400
Re: Instantiate an abstract class Rob McDonald <rob.a.mcdonald@gmail.com> - 2011-04-05 14:54 -0700
Re: Instantiate an abstract class Patricia Shanahan <pats@acm.org> - 2011-04-05 15:09 -0700
Re: Instantiate an abstract class Lew <noone@lewscanon.com> - 2011-04-06 12:43 -0400
Re: Instantiate an abstract class "John B. Matthews" <nospam@nospam.invalid> - 2011-04-06 22:07 -0400
Re: Instantiate an abstract class Roedy Green <see_website@mindprod.com.invalid> - 2011-04-05 19:02 -0700
Re: Instantiate an abstract class Roedy Green <see_website@mindprod.com.invalid> - 2011-04-05 19:42 -0700
Re: Instantiate an abstract class Lothar Kimmeringer <news200709@kimmeringer.de> - 2011-04-06 18:26 +0200
Re: Instantiate an abstract class Lew <noone@lewscanon.com> - 2011-04-06 12:49 -0400
Re: Instantiate an abstract class Patricia Shanahan <pats@acm.org> - 2011-04-06 10:09 -0700
Re: Instantiate an abstract class Lothar Kimmeringer <news200709@kimmeringer.de> - 2011-04-07 01:01 +0200
Re: Instantiate an abstract class Wojtek <nowhere@a.com> - 2011-04-06 12:41 -0700
csiph-web