------Quote of the Day------

The Universal Truth

Tell a man there are 300 billion stars in the universe and he'll believe you. Tell him a bench has wet paint on it and he'll have to touch to be sure.

-Murphy

Thursday, January 21, 2010

Finding Relationship among Classes

A Relationship may belongs to more than one Relation type at same time.In this case our answer should be the Strongest among them.

Dependency --> Association -->Aggregation--> Composition
-------------------------------------------------------------->
Strongness

For Example a relation b/w class A and B is as follows.
Dependency and Association and Aggregation and Composition

So in this case our answer should be Composition .




Now let's See how to Find Relationship among Classes:

1. If Class A is related to class B by any means there is dependency b/w class A and class B.


Eg:class A:


class A
{


void methodA( B b, int i )
{
System.out.print("i am B");
}


}


2. Now apart from Dependency a relation may be of type Inheritance or Association.

a> If there is any keyword 'extends ' is being used to relate these classes than it would be "Inheritance".

b> Else if class A Contains or Stores object of class B it would be "Association".

Else it would be only "Dependency".


3.If it is Association than

a> if Object of Class B is being created in Class A it Composition.


b> Else Object of Class B is passing as Parameter in any function of Class A
it is called "Aggregation ".


Here are some Examples to make things clear.

What is the relationship b/w class A and class B ?

class A {

public void methodA() {

B b = new B();

b.methodB();

}

}

class B {

public void methodB() {

}

}



Ans.1 Dependency

What is the relationship b/w class A and class B and b/w class A and C ?


Class A
{

private B b;


method( C c , int num);

}



Ans.2

Relationship between A and B is Association.
Relationship between A and C is Dependency.

Note: There is not any Difference b/w Association and Aggregation/Composition at Impementation level.


Note: For Aggregation and Composition classes should have 'Part - of' relationship.

For Any queries leave your Comment.
- Agam Gupta.

10 comments:

Anonymous said...

Thanks for sharing

Anonymous said...

very nice explanation for relationships which almost all of us find difficult so thanks a lot for helpings.keep it on u get get its fruits.

Anonymous said...

very good agam...this will increase your demand among girls..:)

agamsays said...

Thanks a lot but that's not the reason why this blog is here.

Mona Lisa said...

Thanks agam. its quite useful.

Anonymous said...

Abe dhang se samjha... Eg 2 & 3zyada clear nahi hua hai mujhe...

agamsays said...

hey! please give me some more details about u r confusion.

And pls mention your name
(i think i know who u r)
-Thanks

Anonymous said...

I just wanted to make a quick comment to say I’m glad I found your blog. Thanks.

Anonymous said...

This was really interesting. I loved reading it

Anonymous said...

This was really interesting. I loved reading it