 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
. Guest
|
Posted: Mon Apr 18, 2005 9:25 am Post subject: array of abstract class |
|
|
Hi all,
is it possible to create an array of an abstract class?
eg if MyClass is abstract, can I declare an array of MyClass?
I know that
MyClass[] myArray = new MyClass[]
will complain that MyClass is abstract and cannot be created, but is there
some other tricky method I can use?
Thanks for your help
Michael
|
|
| Back to top |
|
 |
Arnaud Berger Guest
|
Posted: Mon Apr 18, 2005 9:35 am Post subject: Re: array of abstract class |
|
|
Hi,
This should work , but you have to provide the size of the array :
MyClass[] myArray = new MyClass[5];
Regards,
Arnaud
"." <.@.com> a écrit dans le message news:
42637cf9$0$374$5a62ac22 (AT) per-qv1-newsreader-01 (DOT) iinet.net.au...
| Quote: | Hi all,
is it possible to create an array of an abstract class?
eg if MyClass is abstract, can I declare an array of MyClass?
I know that
MyClass[] myArray = new MyClass[]
will complain that MyClass is abstract and cannot be created, but is there
some other tricky method I can use?
Thanks for your help
Michael
|
|
|
| Back to top |
|
 |
Thomas G. Marshall Guest
|
Posted: Tue May 10, 2005 11:12 pm Post subject: Re: array of abstract class |
|
|
.. coughed up:
| Quote: | Hi all,
is it possible to create an array of an abstract class?
eg if MyClass is abstract, can I declare an array of MyClass?
I know that
MyClass[] myArray = new MyClass[]
will complain that MyClass is abstract and cannot be created, but is
there some other tricky method I can use?
|
Here is a quick example:
public static abstract class A{ abstract void hello(); }
public static class B extends A { void hello(){} }
public static void main(String[] args)
{
A a[] = new A[10];
}
Now the reason that this works is that this line:
A a[] = new A[10];
Simply builds the array object only (which essentially gets it ready to hold
10 elements of concrete subclasses of A). It does not attempt to
instantiate A itself in any way. You need to fill the 10 elements of a[]
with concrete subclasses of A yourself (10 of them).
Remember that as a strict rule, the following:
Thing thing[] = new Thing[10];
only creates the thing[] array object without actually putting any Thing
objects in it.
--
Framsticks. 3D Artificial Life evolution. You can see the creatures
that evolve and how they interact, hunt, swim, etc. (Unaffiliated with
me). http://www.frams.alife.pl/
|
|
| Back to top |
|
 |
Dale King Guest
|
Posted: Tue May 17, 2005 1:46 am Post subject: Re: array of abstract class |
|
|
Thomas G. Marshall wrote:
| Quote: |
Now the reason that this works is that this line:
A a[] = new A[10];
Simply builds the array object only (which essentially gets it ready to hold
10 elements of concrete subclasses of A).
|
I think it is important to be precise here and not give a newbie a false
impression that an array actually holds the objects. Particularly since
he has already demonstrated confusion on that point in his original post.
With all the pass-by-reference confusion I emphasize that there is no
variable (or array) that ever actually holds an object.
So I would say to the OP that the reason this works is that the line
builds an array object that holds 10 references which are all
initialized to null. There is no problem with the array being abstract
because arrays do not hold objects, just references to them.
--
Dale King
|
|
| Back to top |
|
 |
Thomas G. Marshall Guest
|
Posted: Tue May 17, 2005 4:23 am Post subject: Re: array of abstract class |
|
|
Dale King coughed up:
| Quote: | Thomas G. Marshall wrote:
Now the reason that this works is that this line:
A a[] = new A[10];
Simply builds the array object only (which essentially gets it ready
to hold 10 elements of concrete subclasses of A).
I think it is important to be precise here and not give a newbie a
false impression that an array actually holds the objects.
Particularly since he has already demonstrated confusion on that
point in his original post.
With all the pass-by-reference confusion I emphasize that there is no
variable (or array) that ever actually holds an object.
|
Yes, but for conversation purposes, it suffices to say that an array holds a
series of objects. I see your point though, certainly, especially given how
much time /I/ spend in particular trying to keep people from straying into
PBR wastelands, and you're right: we're all in a newbie-rich ng here.
But life is too long to spend it pointing out that
String strs[] = {"one", "two", "three", "four"};
is /not/ "an array of strings". :)
| Quote: | So I would say to the OP that the reason this works is that the line
builds an array object that holds 10 references which are all
initialized to null. There is no problem with the array being abstract
|
The "array" is not abstract
| Quote: | because arrays do not hold objects, just references to them.
|
I'm sorry dale, but this explanation IMHO (stressing the H) is worse. The
fact that the arrays hold references to objects and not the objects
themselves is not detailed enough of an explanation as to /why/ it's
possible to construct an array to seemingly hold references to something
that by themselves cannot be instantiated.
The reason that
AbstractClass ac[] = new AbstractClass[10];
works is that there is never a single AbstractClass ever attempted to be
created. Not one. The /only/ thing created is an array meant to hold refs
to either instantiations of AbstractClass, or of concrete subclasses of
AbstractClass, and /since/ you cannot instantiate an abstract class, only
the latter is allowed.
If java actually allowed the array to be built with default instantiations
(instead of nulls), then references or not, the AbstractClass example above
would not be allowed. So that it holds references does not by itself grant
the ability to create the array.
While we're at it, another thing that we might want to stress to a newbie is
this: Suppose that the class Thing has two constructors: Thing(int) and
Thing(String). The array of Things is still declared and defined without
any constructor specifier, because no Thing itself is created:
Thing thing[] = new Thing[10];
In other words, the following does not, nor could ever, make any sense:
// no no no...
Thing thing[] = new Thing("hello")[10]; // Array of 10 Things that
take "hello"
It may seem silly, but it is a concept that I've seen newcomers trip over.
This little example is useful in that regard:
Thing thing[] = new Thing[10];
thing[0] = new Thing("hello");
thing[1] = new Thing(100);
(...)
--
Framsticks. 3D Artificial Life evolution. You can see the creatures
that evolve and how they interact, hunt, swim, etc. (Unaffiliated with
me). http://www.frams.alife.pl/
|
|
| Back to top |
|
 |
Dale King Guest
|
Posted: Wed May 18, 2005 2:22 am Post subject: Re: array of abstract class |
|
|
Thomas G. Marshall wrote:
| Quote: | Dale King coughed up:
Thomas G. Marshall wrote:
Now the reason that this works is that this line:
A a[] = new A[10];
Simply builds the array object only (which essentially gets it ready
to hold 10 elements of concrete subclasses of A).
I think it is important to be precise here and not give a newbie a
false impression that an array actually holds the objects.
Particularly since he has already demonstrated confusion on that
point in his original post.
With all the pass-by-reference confusion I emphasize that there is no
variable (or array) that ever actually holds an object.
Yes, but for conversation purposes, it suffices to say that an array holds a
series of objects. I see your point though, certainly, especially given how
much time /I/ spend in particular trying to keep people from straying into
PBR wastelands, and you're right: we're all in a newbie-rich ng here.
But life is too long to spend it pointing out that
String strs[] = {"one", "two", "three", "four"};
is /not/ "an array of strings".
|
I agree in normal conversation we confuse the distinction. The fact that
the OP asked the question proved that the distinction was not clear to him.
| Quote: | So I would say to the OP that the reason this works is that the line
builds an array object that holds 10 references which are all
initialized to null. There is no problem with the array being abstract
The "array" is not abstract
|
Sorry, wrote that too late in the evening.
| Quote: | because arrays do not hold objects, just references to them.
I'm sorry dale, but this explanation IMHO (stressing the H) is worse. The
fact that the arrays hold references to objects and not the objects
themselves is not detailed enough of an explanation as to /why/ it's
possible to construct an array to seemingly hold references to something
that by themselves cannot be instantiated.
|
But it is an important part of the explanation. The reason that creating
the array is not creating instances is that arrays only hold references
and not objects.
| Quote: | The reason that
AbstractClass ac[] = new AbstractClass[10];
works is that there is never a single AbstractClass ever attempted to be
created. Not one. The /only/ thing created is an array meant to hold refs
to either instantiations of AbstractClass, or of concrete subclasses of
AbstractClass, and /since/ you cannot instantiate an abstract class, only
the latter is allowed.
|
And I agree with that wording. When you said that the array will hold
elements of concrete subclasses that is adding another layer of
confusion that is not quite correct.
--
Dale King
|
|
| Back to top |
|
 |
Thomas G. Marshall Guest
|
Posted: Wed May 18, 2005 5:53 pm Post subject: Re: array of abstract class |
|
|
Dale King coughed up:
| Quote: | Thomas G. Marshall wrote:
Dale King coughed up:
|
....[rip]...
| Quote: | because arrays do not hold objects, just references to them.
I'm sorry dale, but this explanation IMHO (stressing the H) is
worse. The fact that the arrays hold references to objects and not
the objects themselves is not detailed enough of an explanation as
to /why/ it's possible to construct an array to seemingly hold
references to something that by themselves cannot be instantiated.
But it is an important part of the explanation. The reason that
creating the array is not creating instances is that arrays only hold
references and not objects.
|
No, again that's just not the right explanation. Java could have easily
said that
Thing[] thing = new Thing[10];
actually created the array to hold 10 references (as it does now) and
actually *created* the 10 instances of Thing. It would therefore disallow
poly-m, and any usage of abstract classes in the formalism, and further it
would (as I showed previously) require some semblance of a constructor call.
But the thing would still be holding references.
The fact that it holds references is /not/ the point. The true /reason/ the
following is allowed:
AbstractThing[] abstractThing = new AbstractThing[10];
is because it does /not/ attempt to create a single instance of the
element's class. No AbstractThing is attempted to be instantiated at all.
--
With knowledge comes sorrow.
|
|
| Back to top |
|
 |
Dale King Guest
|
Posted: Thu May 19, 2005 4:00 am Post subject: Re: array of abstract class |
|
|
Thomas G. Marshall wrote:
| Quote: | Dale King coughed up:
Thomas G. Marshall wrote:
Dale King coughed up:
....[rip]...
because arrays do not hold objects, just references to them.
I'm sorry dale, but this explanation IMHO (stressing the H) is
worse. The fact that the arrays hold references to objects and not
the objects themselves is not detailed enough of an explanation as
to /why/ it's possible to construct an array to seemingly hold
references to something that by themselves cannot be instantiated.
But it is an important part of the explanation. The reason that
creating the array is not creating instances is that arrays only hold
references and not objects.
No, again that's just not the right explanation. Java could have easily
said that
Thing[] thing = new Thing[10];
actually created the array to hold 10 references (as it does now) and
actually *created* the 10 instances of Thing. It would therefore disallow
poly-m, and any usage of abstract classes in the formalism, and further it
would (as I showed previously) require some semblance of a constructor call.
But the thing would still be holding references.
|
But it could be argued that the reason it is not creating instances is
that it is just a reference holder.
But there is not much point to arguing further as the issue is what was
clearest for the OP.
The horse is dead, let's stop beating it.
--
Dale King
|
|
| Back to top |
|
 |
Thomas G. Marshall Guest
|
Posted: Thu May 19, 2005 10:32 pm Post subject: Re: array of abstract class |
|
|
Dale King coughed up:
| Quote: | Thomas G. Marshall wrote:
Dale King coughed up:
Thomas G. Marshall wrote:
Dale King coughed up:
....[rip]...
because arrays do not hold objects, just references to them.
I'm sorry dale, but this explanation IMHO (stressing the H) is
worse. The fact that the arrays hold references to objects and not
the objects themselves is not detailed enough of an explanation as
to /why/ it's possible to construct an array to seemingly hold
references to something that by themselves cannot be instantiated.
But it is an important part of the explanation. The reason that
creating the array is not creating instances is that arrays only
hold references and not objects.
No, again that's just not the right explanation. Java could have
easily said that
Thing[] thing = new Thing[10];
actually created the array to hold 10 references (as it does now) and
actually *created* the 10 instances of Thing. It would therefore
disallow poly-m, and any usage of abstract classes in the formalism,
and further it would (as I showed previously) require some semblance
of a constructor call. But the thing would still be holding
references.
But it could be argued that the reason it is not creating instances is
that it is just a reference holder.
But there is not much point to arguing further as the issue is what
was clearest for the OP.
The horse is dead, let's stop beating it.
|
Agreed.
--
Puzzle: You are given a deck of cards all face down
except for 10 cards mixed in which are face up.
If you are in a pitch black room, how do you divide
the deck into two piles (may be uneven) that each
contain the same number of face-up cards?
Answer (rot13): Sebz naljurer va gur qrpx, qrny bhg
gra pneqf naq syvc gurz bire.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|