For Programmers: Free Programming Magazines  


Home > Archive > Java Help > October 2004 > Arrays of multiple types of objects possible?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Arrays of multiple types of objects possible?
Pat

2004-10-27, 3:58 am

Is it possible to create an array of multiple data types, specifically
seperate instances of different objects? What i am trying to do is to
create a very basic wargame and in it I would like to include
transports. Now I want the transports to carry 2 soldiers or a tank
just like in Axis and Allies so I created a class for the Transport and
within that class I need to track what the cargo is, if there is any.
I thought an array would be good to hold this but it may be that i need
2 seperate variables to hold the cargo(tank or infantry).

Hal Rosser

2004-10-27, 3:58 am


"Pat" <pat@patandsheri.com> wrote in message
news:1098843797.440061.125930@c13g2000cwb.googlegroups.com...
> Is it possible to create an array of multiple data types, specifically
> seperate instances of different objects? What i am trying to do is to
> create a very basic wargame and in it I would like to include
> transports. Now I want the transports to carry 2 soldiers or a tank
> just like in Axis and Allies so I created a class for the Transport and
> within that class I need to track what the cargo is, if there is any.
> I thought an array would be good to hold this but it may be that i need
> 2 seperate variables to hold the cargo(tank or infantry).
>

That's one strong point of java
assume a class Pet
and class Dog extends Pet
and class Cat extends Pet
you can have an array of type Pet that holds Dog and Cat objects
assuming you defined a speak() method in Pet and over-rode that method in
Cat and Dog, you can directly access the speak() method from a Pet instance
of the array.
myPet[1].speak(); // if myPet[1] is a cat then output is "meow" - if its a
dog, then output is "woof" - assuming they were defined that way in the
class.
make sense?



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.781 / Virus Database: 527 - Release Date: 10/21/2004


Pat

2004-10-27, 3:59 pm

Thanks for the input gang, that one was driving me crazy. I actually
thought about it and i think that my design might be flawed too but
for now, since I am just starting out, it is the simplest I can come up
with. I am sure as I get more Java experience under my belt that it
will chaange. Thanks again :)

Thomas G. Marshall

2004-10-27, 3:59 pm

Hal Rosser coughed up:
> "Pat" <pat@patandsheri.com> wrote in message
> news:1098843797.440061.125930@c13g2000cwb.googlegroups.com...
> That's one strong point of java
> assume a class Pet
> and class Dog extends Pet
> and class Cat extends Pet
> you can have an array of type Pet that holds Dog and Cat objects
> assuming you defined a speak() method in Pet and over-rode that
> method in Cat and Dog, you can directly access the speak() method
> from a Pet instance of the array.
> myPet[1].speak(); // if myPet[1] is a cat then output is "meow" - if
> its a dog, then output is "woof" - assuming they were defined that
> way in the class.
> make sense?


Well, that's a strong point of OO, not of java in particular.

And it would require that all things that can be taken as cargo be part of
the same inheritance hierarchy, which all by itself smells of bad design
because they all might need their own inheritance trees and otherwise be
utterly unrelated, hence my suggestion of interfaces to unite all things
storable as cargo as a Cargo "type".






--
"His name was Robert Paulson. His name was Robert Paulson. His name was
Robert Paulson..."


Hal Rosser

2004-10-27, 8:57 pm


"Thomas G. Marshall" < tgm2tothe10thpower@replacetextwithnumber
.hotmail.com>
wrote in message news:7WPfd.4070$9R4.3089@trndny09...
> Hal Rosser coughed up:
>
> Well, that's a strong point of OO, not of java in particular.
>
> And it would require that all things that can be taken as cargo be part of
> the same inheritance hierarchy, which all by itself smells of bad design
> because they all might need their own inheritance trees and otherwise be
> utterly unrelated, hence my suggestion of interfaces to unite all things
> storable as cargo as a Cargo "type".
>

You're , of course exactly correct - and I was of course entirely incorrect.
I was trying a simpler explanation which I thought may be more directly
applicable to the OP's question.
since java is OO, then it is a strong point of java - since java (shall we
say) "inherits" OO's strong points.
but of course it could have been misconstrued as an implied exclusive



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.781 / Virus Database: 527 - Release Date: 10/21/2004


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com