Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Arrays of multiple types of objects possible?
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).


Report this thread to moderator Post Follow-up to this message
Old Post
Pat
10-27-04 08:58 AM


Re: Arrays of multiple types of objects possible?
"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



Report this thread to moderator Post Follow-up to this message
Old Post
Hal Rosser
10-27-04 08:58 AM


Re: Arrays of multiple types of objects possible?
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 :)


Report this thread to moderator Post Follow-up to this message
Old Post
Pat
10-27-04 08:59 PM


Re: Arrays of multiple types of objects possible?
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..."



Report this thread to moderator Post Follow-up to this message
Old Post
Thomas G. Marshall
10-27-04 08:59 PM


Re: Arrays of multiple types of objects possible?
"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



Report this thread to moderator Post Follow-up to this message
Old Post
Hal Rosser
10-28-04 01:57 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Java Help archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:58 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.