• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

has-a relationship

 
saima kanwal
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 ??
 
Jim Hoglund
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...
 
Stephen Davies
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic