Anda di halaman 1dari 7

In Object-Oriented programming, an Object communicates to other Object to use functionality

and services provided by that object.

Delegate functionality to another class (Association)

Association is when you hand over the responsibilty for a perticular task to another class or
method. Association establish the relationship b/w two classes using through their objects

Association can be represented by a line between these classes with an arrow indicating the
navigation direction. In case arrow is on the both sides, association has bidirectional
association. In Association there is no Whole-Part relationship among objects sharing each
others behaviors. They just want to use some functionality of other object.

Here Employee uses Manager class behaviour "Manager_Info" and both Manager and
Employee class have references of each type so that they can get functionality of eachother.So
we can say both Employee and Manager are associated with eachother.
class Program
static void Main(string[] args)
Manager manager = new Manager();
manager.manager_name = "John";

Employee emp = new Employee();

emp.emp_name = "Harry";
class Employee
public string emp_name;
public void Manager_Name(Manager m)
class Manager
public string manager_name;
public void manager_Info(Employee e)
Console.WriteLine("Manager of Employee "+e.emp_name +" is " + manager_name);}

Association is best used when you use another class's functionality,as is,without changing that
behavior at all.

If you need to use functionality in another class but you don't want to change
that functionality consider using Association instead of inheritance.
Composition is a part-of relationship. Composition means one object is composed of several
different objects and those different objects are part of the composed object. Composition is
'Has-A' relationship between objects.

- Pizza is actually a great example of composition.It's composed of diiferent ingredients.

When the pizza is gone, so are the ingredients...

There is one important point about composition. When an object is composed of other objects,
and the owning object is destroyed, the objects that are part of the composition go away too.
That's a little confusing ,so let's take a closer look at what that actually means.

- Lets take an example of human and heart. A human needs heart to live and a heart needs a
Human body to survive. In other words when the One class is an owner and other classes are
part of the owner and they both are dependent on each other and their life span are same (if
one dies then another one too) then its a composition.

-Let's take another example. Car and Engine. Every Car must has an Engine. We can say Car is an
owner and composed of Engine (which is part of Car) .If Car destroys ,engine will also destroy.
class Program
static void Main(string[] args)
Car car = new Car();
car.color = "White";
car.modelno = "aa11";
class Car
public string color="";
public string modelno = "";
public void CarInfo()
Console.WriteLine("Color of car is " + color + "\n"+ "Model of car is"
+modelno );
Engine e = new Engine(); //Engine object inside Car class
class Engine
public void EngineDetails()
Console.WriteLine("This is car engine...");

What happens if this Car is destroyed? Obviously ,the Engine will also be destroyed .Because it
was instantiated inside Car. It does not exist outside of the car object. If you get rid of the Car
object then you are automatically getting rid of Engine object associated with car, too.

 In composition , the object composed of other behaviors owns those behaviors. When
the object is destroyed, so are all of its behaviors. The behaviors in a composition do
not exist outside of the composition itself.

 Composition is really about ownership. The main object owns the composed behavior,
so if that object goes away, all the behavior does, too.
What happens when you want all the benefits of composition-flexibility in choosing a behavior
,but your composed objects need to exist outside of your main object? There is where
aggregation comes in. Aggregation is also 'Has-A' relationship.

Aggregation is when one class is used as part of other class, but still exists outside of that other
class. And it is not necessary that the owner always contains those part objects. The objects
may or may or be a part of the whole. In aggregation the objects have their own life-cycle but
they do have ownership as well. Where as in association there is no ownership among objects
sharing each other's behaviors. This is difference in Association and aggregation.

-The ice-cream , bananas, and cherries exist outside of a banana split. Take away that fancy
container, you are still got the individual components. As well as banana split can exist without
having cherries etc in it.

-Relationship between a Library and books is aggregation because Books are part of Whole
Library ,But if we remove Books from Library, the Library still exists but it is just an empty
Library, Similarly Books can also place in some other place .

-Relation between Department and Professor, each department has a number of professors. If
the Department close , the professors in those departments will continue to exist. Therefore,
departments have an aggregation of professors. Professors are part of Whole Department and
the lifetime of professor does not depends on Department's Lifetime.
class Department
public string departname;
public void DeptInstructorInfo(Instructor i)
Console.WriteLine("Instructor details of Department:");
class Instructor
public string designation;
public string name;
public void InstructorInfo()
Console.WriteLine("Instructor name is " +name +" Designation: "

class Program
static void Main(string[] args)
Department d = new Department();
d.departname = "CS";
Instructor i = new Instructor(); = "Abc";
i.designation = "Xyz";
Here if we destroy Department object , there will be no effect on Instructor's object.
Instructor's object lifetime does not depend on Department's Lifetime.

Compostion and Aggregation:

A university owns various departments, and each department has a number of professors. If
the university closes, the departments will no longer exist, but the professors in those
departments will continue to exist. Therefore, a University can be seen as a composition of
departments, whereas departments have an aggregation of professors. In addition, a
Professor could work in more than one department, but a department could not be part of
more than one university.