Reputation: 236
So I have seen in different tutorials, or examples online of different classes using a composition of multiple classes to create a more complex object. I understand most of the creation of said objects, but one part I have seen baffles me, and I'm not sure how you would describe this type of class which is why I am finding difficulty researching it online.
An example of what I'm looking for would be
public class House{
private Kitchen kitchen;
private LivingRoom lRoom;
private Bedrooms[] bedrooms;
public House(...){}
}
The part of this that I don't understand is how to create the 'Bedrooms[]' class. Like if its an array, would it just be
Bedrooms[] rooms = new Bedrooms[5];
and this array would be created inside of a Bedrooms class? Please help shed light. If I haven't explained my confusion well enough, I can try to re-explain.
Upvotes: 0
Views: 65
Reputation: 2454
Since you've already created a House
class, you know how to create classes. (That's stated for the fact of stating the obvious, and so this isn't a 1 line answer.)
All you need to do is to create a Bedroom
class. Java will do the rest to make it into an array. All it is is multiple instances of the Bedroom
class, which you can put different data into, just like any other array.
Upvotes: 0
Reputation: 1187
What you have is nearly already accomplishing your desired result. You wouldn't create a "Bedrooms[]
" class, but you would create a Bedroom
(singular) class to model a single bedroom, then have an array of Bedroom
instances from that class you made. Ex)
Bedroom[] rooms = new Bedroom[5]; // You now have an array of 5 Bedroom objects in House.
Upvotes: 1