Sunday, December 14, 2014

[Java] Java HAS-A relationship sample

According from the definition in Wikipedia, has-a (has_a or has a) is a composition relationship where one object (often called the constituted object, or part/constituent/member object) "belongs to" (is part or member of) another object (called the composite type), and behaves according to the rules of ownership. In simple words, has-a relationship in an object is called a member field of an object. Multiple has-a relationships will combine to form a possessive hierarchy.

There is 3 classes provided:
  1. class Class1 {
  2. String v1;
  3. }
  4. class Class2 { 
  5. Class1 c1;
  6. String v2;
  7. }
  8. public class Class3 {
  9. Class2 c1;
  10. String i3;
  11. }

1 ) Class3 HAS-A Class2 (line9)

2) Class2 has-a Class1 (line5)

3) Since the string value v1 was initialized at Class 1, (line2)
Class3 HAS-A Class2, (line9)
Class2 HAS-A Class1, (line5)
so Class3 HAS-A string v1 (since each object of Class1 has-a v1.).


No comments :

Post a Comment