Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #557

Re: Instantiate an abstract class

From Lew <noone@lewscanon.com>
Newsgroups comp.lang.java.help
Subject Re: Instantiate an abstract class
Date 2011-04-06 12:43 -0400
Organization albasani.net
Message-ID <ini58c$8eh$2@news.albasani.net> (permalink)
References <61c7300d-6011-4639-a5f1-5cd4c58906fa@r4g2000prm.googlegroups.com> <ing2nd$627$1@dont-email.me> <edce4c55-5560-4718-b3b2-d9797dff79fc@l39g2000yqh.googlegroups.com>

Show all headers | View raw


Rob McDonald wrote:
> Joshua Cranmer wrote:
>> 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();

> That worked great.  I don't know why I hadn't come up with that
> solution myself.
>
> 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.

Because you haven't yet read /Effective Java/ by Joshua Bloch.  You must.  We 
all must, and not just once, either.

<http://java.sun.com/docs/books/effective/>
The very first item of which is,
"Item 1: Consider static factory methods instead of constructors"

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

Back to comp.lang.java.help | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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