Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com Jul 16 at 23:37

This question came from our site for professional and enthusiast programmers.

1 Answer 1

up vote 0 down vote accepted

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 :)

share|improve this answer
    
A library is not a type of school. However, there might be a library in a school. In contrast, a high school or a college would be types of schools. That being said, sometimes, using inheritance can make the code more complicated and can easily be replaced with composition (ex. A single school class that has a model that describes the type of school). –  unholysampler Jul 17 at 12:41
    
Thank you for the detailed explanation. It seems though that in order to tell the difference you would need a lot of prior knowledge in order to figure out the answer. Here is an example question: public class Store{MerchandiseItem item; public Store(){item = new MerchandiseItem();}} They say that is composition, but couldn't a MerchandiseItem exist outside the store? like in a warehouse or a factory? –  user1020788 Jul 17 at 15:20
    
Don't get too complicated. They mentioned Store class. Store must need merchandise to sell. Thats the matter you should remember. Not they talked about warehouse or factory. If They Introduce Warehouse or Factory classes then they may vary depends upon what do you equals (Workers of warehouse, Manufaturing products and so on) with them. –  Ramachandra Sah Jul 17 at 16:22
    
Try Mala Gupta for your OCJA se 7 exam. All The Best :) –  Ramachandra Sah Jul 17 at 16:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.