Differences between Direct, Composition, Aggregation, and Temporary Association

I am looking to take my java certification very soon, and I have a question about the different types of associations described in the official oracle guide book.

The book describes the 4 as the following:

  • Direct = “has-a”
  • Composition = “composed-of”
  • Aggregation = “part-of”
  • Temporary = method parameters or local variables inside a method etc..

I understand the examples they provide, but when shown a piece of code I can never figure out why it is one type over another. To further my frustration these don’t seem to be common terms in uml diagrams and composition or aggragate is usually described as a “has-a” relationship depending on where you read.

Here is a link to the book. http://www.amazon.com/Programmer-Study-Guide-1z0-803-Oracle/dp/0071789421

Pages 386-388 describe the different associations. Code examples would be very helpful.

HAS-A Relationship

Has-A means an instance of one class “has a” reference to an instance of another class or another instance of same class.
It is also known as “composition” or “aggregation”.
There is no specific keyword to implement HAS-A relationship but mostly we are depended upon “new” keyword.

Composition :

Without existence of container object, if there is no chance of existence of contained objects then container and contained objects are said to be strongly associated and this strong association is known as composition.
Eg: A “university” has several “departments”. Without existence of “university” there is no chance for the “departments” to exist. Hence “university” and “departments” are strongly associated and this strong association is known as composition.

Aggregation

Without existence of container object, if there is a chance of existence of contained objects then container and contained objects are said to be loosely associated and this strong association is known as aggregation.
Eg: A “department” has several “professors”. Without existence of “departments” there is good chance for the “professors” to exist. Hence “professors” and “department” are loosely associated and this loose association is known as Aggregation.

Example For “Has-A” and “is-A” Relationship:

Class Library extends School

   {

          Book b=new Book();

          CD cc=new CD(); 

   } 

Let us take one example.

Has-A:

    Library needs book and cd to run library. If you don't have book and cd then it 

can’t be a Library. So Library “has a” relationship with Book and CD.

IS-A:

    Assume that Library placed inside a school and prohibited to use Third persons 

(outside) and only school person can able to use that Library. So Library(class) “is a”

relationship with School(class).

Temporary:

    1.Temporary variable or Local Variables or automatic variables have its lifetime  

within blocks ( i.e { and } braces), methods and constructors.

    2.Access modifiers cannot be used for local variables.

    3. Only final (non-access modifier) maybe used for local variables.

Lets finish this with one small example program:

class TestAge {

int age; // Instance Variable age

public void exampleAge() {
    int age = 7; // Local Variable;
    System.out.println(" Age is : " + age); // prints 7,  Local variable age.
}

public static void main(String args[]) {
    TestAge test = new TestAge();
    test.exampleAge();
    test.age = 10; // accessing Instance Variable
    System.out.print(test.age); // prints 10, Instance Variable age. 

}

}

output:
Age is : 7 10

Hope you understood. Thank You 🙂

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật