This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

has-a relationship  RSS feed

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A question from SCJP K & B Master exam C :

which statements are true:

..

among the statements, One is :"Has-A relationships always rely on instance variables "(this according to the answer is true)

What about static variables? if a class has static variable is it not considered to be a has-a relationship? can anyone explain??


another question that i want to clarify: consider the following code written by me:




1). Is it a has-a relationship (class A has- a Integer) since Integer is in the standard API. ???

2). If we are looking for coupling then class A and Integer are not tightly coupled because Integer is in standard API . correct or not ??
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's from an OOP perspective that we most often talk about "has-a" and "is-a"
relationships. So though you are technically correct, that a class also will have
variables and constants, the preferred answer talks about objects and inheritance.

Jim...
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static variables are class level variables and are initialized before the actual class object, is created. Jim is right, and it can be a little confusing but has-a and is-a jargon refers to the instantiation of objects on the heap and from the perspective any relationships between those objects such as in inheritance. In this sense a class object once instantiated as an object doesn't own static variables, they belong to the class itself and as such are shared by ALL instances of the class, not just one class object.

 
Cob is sand, clay and sometimes straw. This tiny ad is made of cob:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!