• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on creating objects

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all - I am new to Java and new to this board. This is my first post. I am attempting to learn Java and I am having an issue with the interaction of Objects. I am working on the classic Elevator problem.
When I create an elevator Object within the constructor I then create floor objects and I am also going to create passenger objects. Is it bad design to create objects within another? It seems you must go all the way back to the first object to refer back to the correct object you created under it. I feel as if I am not understanding something basic. The elevator constuctor and floor object creation is below.

 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it is not bad design to 'construct' objects on a constructor. After all, that is what a constructor is for.

I think creating floor objects when creating an elevator makes sense. Floors, hopefully, will not get created or destroyed during the existence of the elevator. But creating passenger objects? Maybe a passenger object container but I would hope that an elevator exists before I get on it :-)
 
Ranch Hand
Posts: 97
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joe Baxter wrote:Hi all - I am new to Java and new to this board. This is my first post. I am attempting to learn Java and I am having an issue with the interaction of Objects. I am working on the classic Elevator ...



There is definately no issue with creating objects in a constructor. Refer to the Object Oriented Programming concepts of aggregation and composition.
 
Joe Baxter
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and welcome to JavaRanch
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you describe is possible, and there may be program designs for which it makes perfect sense, but in the specific case of an elevator program, it may be silly. E.g., how does creating an elevator object add floors to the building? Likewise, why would destroying an elevator object remove floors?

Think or work through your design to see if what you envision makes sense. I think as you work it through to completion these kind of questions will answer themselves.
 
Gaurav Sagar
Ranch Hand
Posts: 97
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Catch Ernie! Catch the egg! And catch this tiny ad too:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic