Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!news-transit.tcx.org.uk!feeder.news-service.com!news-out2.kabelfoon.nl!newsfeed.kabelfoon.nl!bandi.nntp.kabelfoon.nl!spln!extra.newsguy.com!newsp.newsguy.com!news3 From: Wojtek Newsgroups: comp.lang.java.help Subject: Re: Instantiate an abstract class Date: Wed, 06 Apr 2011 12:41:58 -0700 Organization: NewsGuy - Unlimited Usenet $19.95 Lines: 41 Message-ID: References: <61c7300d-6011-4639-a5f1-5cd4c58906fa@r4g2000prm.googlegroups.com> NNTP-Posting-Host: pd69406af5e013e31ec1d732b84c4dfd6f76e331ecfae9d48.newsdawg.com Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-15"; format=flowed Content-Transfer-Encoding: 8bit X-Newsreader: MesNews/1.08.03.00-gb Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.help:562 Rob McDonald wrote : > abstract class AFoo { > void doStuff() { AFoo a = getNewInstance(); > } public absract AFoo getNewInstance(); > } > > class Bar extends AFoo { @Overrides public Bar getNewInstance() { return new Bar(); } > } > > class Fred extends AFoo { @Overrides public Fred getNewInstance() { return new Fred(); } > } So "AFoo a" will hold an instance of whatever class extends AFoo, yet it can be cast into the subclass. -- Wojtek :-)