Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #539
| From | Joshua Cranmer <Pidgeot18@verizon.invalid> |
|---|---|
| Newsgroups | comp.lang.java.help |
| Subject | Re: Instantiate an abstract class |
| Date | 2011-04-05 17:48 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <ing2nd$627$1@dont-email.me> (permalink) |
| References | <61c7300d-6011-4639-a5f1-5cd4c58906fa@r4g2000prm.googlegroups.com> |
On 04/05/2011 05:14 PM, Rob McDonald wrote:
> Now, we all know that you can't instantiate an abstract class -- but I
> want to anyway... Actually, I would like my abstract class to be able
> to instantiate another instance of whatever concrete class it is at
> the time.
Your class is abstract, so you can easily have:
abstract class AFoo {
public abstract AFoo makeInstance();
}
Or, as Patricia mentions:
abstract class AFoo {
public abstract AFoo clone(); // Legal only in Java 5+
}
Each subclass would then implement this method as appropriate.
I can answer your question more directly with reflection, but it's a
method I wouldn't recommend without a very compelling reason why this
approach wouldn't work for you.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
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