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