Tam Lam
Tam Lam

Reputation: 135

Array to store objects of two different classes?

I am a beginner in java and programming in general.Basically, I am wondering whether if it is possible to store objects of different classes in the same array. For example, instead of create 2 seperate arrays like this:

//AssembledPart is subclass of Part
Part[] part = new Part[4];
    part[0] = new Part("p101", "Crank", 218, 12.20);
    part[1] = new Part("p102", "Pedal", 320, 14.30);
    part[2] = new Part("p103", "Handlebar", 120, 35.50);
    part[3] = new Part("p104", "Stem", 90, 20.00);

    AssembledPart[] asPart = new AssembledPart[2];
    asPart[0] = new AssembledPart("p183", "Crank-Pedal", 80, 3.50, part[0], part[1]);
    asPart[1] = new AssembledPart("p184", "Handlebar-Stem", 30, 1.50, part[2], part[3]);

Is there any other way to combine them in to just one array.If there is a way, how can I do it.

Upvotes: 2

Views: 11850

Answers (4)

Evandro Pomatti
Evandro Pomatti

Reputation: 15094

From the official documentation Java Arrays:

An array is a container object that holds a fixed number of values of a single type...

The short answer is that you cannot use different classes.

However, the trick here is that you can use object orientation so that an array is created for a more generic class which allows for subclasses to be set in the array.

You mentioned that AssembledPart is a subclass of Part, and that means you are allowed to set a AssembledPart object into a Part array. Like this:

    Part[] yourArray = new Part[2];
    yourArray[0] = new Part();
    yourArray[1] = new AssembledPart();

This very same concept will work not only when using collections but in many other object usage, in virtually all object oriented languages.

I suggest you to read more in Object-Oriented Programming Concepts, will clear your mind in some of this concepts =).

Upvotes: 2

user619237
user619237

Reputation: 1892

You can create an interface IPart and implement that in both Part and AssembledPart. that way you can create an array of that interface and use that array to store both Part and AssembledPart object there.

interface IPart {
}

class Part implements IPart {
}

class AssembledPart implements IPart {
}
// in code you can do following...
IPart[] parts = new IPart[2];
parts[0] = new Part(...);
parts[1] = new AssembledPart(...);

you can also do it by extending the AssembledPart

 class Part {
 }
 class AssembledPart extends Part{
 }
 // in code..
 Part[] parts = new Part[2];
 parts[0] = new Part(...);
 parts[1] = new AssembledPart(...);

another way you can do it by using array of Object

 Object[] parts = new Object[2];
 parts[0] = new Part(...);
 parts[1] = new AssembledPart(...);

but you need to understand why you want to merge two array and how you want to merge it.

fyi, at some point you may need to check what kind of object you have in the array. you can do it by using.

  if(parts[0] instance of AssembledPart) {
     // do something.
  }

Upvotes: 1

ApproachingDarknessFish
ApproachingDarknessFish

Reputation: 14313

If AssembledPart is a subclass of Part (as you indicated in the comments), then what you're asking is trivial. Subclasses can always be assigned to their parent classes, so all you need to do is:

Part[] part = new Part[6];
part[0] = new Part("p101", "Crank", 218, 12.20);
part[1] = new Part("p102", "Pedal", 320, 14.30);
part[2] = new Part("p103", "Handlebar", 120, 35.50);
part[3] = new Part("p104", "Stem", 90, 20.00);
part[4] = new AssembledPart("p183", "Crank-Pedal", 80, 3.50, part[0], part[1]);
part[5] = new AssembledPart("p184", "Handlebar-Stem", 30, 1.50, part[2], part[3]);

Upvotes: 2

David Meredith
David Meredith

Reputation: 64

No, you cannot have different objects in the same array. When you create an object array, you specify a certain object to be put into that array. You'll have to create different object arrays for different classes.

Upvotes: 1

Related Questions