Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Class Relationship Question  RSS feed

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What class relationship do AAA and BBB have in the code below?




I would say composition. With the argument that class AAA contains objects of the class BBB.

Apparently not, the right answer should be dependency or association.

How come it's not composition?
 
Bartender
Posts: 5293
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"_b" is not a member variable (aka field), therefore it is not composition. You can't say that AAA has-a BBB. "_b" comes into existence inside AAA's constructor, but is only used as a local variable, and then disappears at the end of the constructor. I would say that the constructor "uses" BBB, or that the constructor perhaps "depends" on BBB, but the class AAA does not really depend on BBB. Once an AAA is constructed, BBB is not a part of the AAA object any longer.

Also, the use of an underscore (_) in a variable name is highly frowned upon. I've only seen this in much older projects, and even those are being undone as they get re-factored.
 
Daniel Andersson
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:"_b" is not a member variable (aka field), therefore it is not composition. You can't say that AAA has-a BBB. "_b" comes into existence inside AAA's constructor, but is only used as a local variable, and then disappears at the end of the constructor. I would say that the constructor "uses" BBB, or that the constructor perhaps "depends" on BBB, but the class AAA does not really depend on BBB. Once an AAA is constructed, BBB is not a part of the AAA object any longer.

Also, the use of an underscore (_) in a variable name is highly frowned upon. I've only seen this in much older projects, and even those are being undone as they get re-factored.


Ok thanks, good explanation! (y)
 
Sheriff
Posts: 12815
211
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code will not even compile. You cannot have any private declarations inside a method or constructor.
 
What does a metric clock look like? I bet it is nothing like this tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!