Joe Baxter wrote:Sorry if my question wasn't clear. I understand that you create an object in a constructor but does it make sense to construct objects of other types when you call a different constructor? Such as when you call an elevator constructor you then create floor and/or passenger objects. Then you must always refer to them as elevator.floor.... etc. Does that makse sense?
Thanks for the quick replies.
Well that's is what I meant, though you can create an object anywhere, what matters is how you are holding the reference to that object. That all comes under the aggregation and the composition.
Your Elevator class could have Floor and Passenger variables to hold references of these type of objects which is aggregation and defines the HAS-A relationship. Alternatively, you may create the Floor and Passenger class into the Elevator class, known as inner classes which would be composition, in that case. Its all a matter of requirements of the design.
Regards,
Gaurav