Click Here

Saturday, 14 May 2016

Aggregation Composition & Encapsulation OOP

 Aggregation  Composition  &  Encapsulation This is also called as composition model. Example in order to make a “Accounts” cl... thumbnail 1 summary
 Aggregation Composition 

Encapsulation

This is also called as composition model. Example in order to make a “Accounts” class it has use other objects example “Voucher”, “Journal” and “Cash” objects. Therefore, accounts class is aggregation of these three objects.


It's has a relation with one owner (Association and aggregation are almost same with just one difference, in aggregation we have one owner but in association there is no owner)
Manager has N Employees for a project. Team has Players.An aggregation is used when life of object is independent of container object But still container object owns the aggregated object.
E.g. Team has players, if team dissolve, Player will still exists.

Life or existence of the aggregated objects are independent of each other, But one object is playing the role of Owner of the other object.

Composition

It's has a relation but it's a specialized form of Aggregation
Composition is a special case of aggregation. In a more specific manner, a restricted aggregation is called composition. When an object contains the other object, if the contained object cannot exist without the existence of container object, then it is called composition.

Class A contains Class B
  • Order consists of Line Items. 
  • Body consists of Arm, Head, and Legs.
  • Bank Account consists of Balance and Transaction History.
·         A composition is used where each part may belong to only one whole at a time.
·         E.g. A line item is part of an order so a line item cannot exist without an order.
Life or existence of the composite object is dependent on the existence of container object, Existence of composite object is not meaningful without its container object.
.

Encapsulation

It is a process of hiding all the internal details of an object from the outside world.
Encapsulation is another pillar of Object Oriented programming. Object should hide its inner working from outside world. Keeping member variables as private and restricting it from exposing to outside world helps objects to avoid unwanted changes to variables. Functionalities of the object is available to outside world thought its public methods only.

The wrapping up of data and its functions into a single unit is called Encapsulation.
When using Data Encapsulation, data is not accessed directly, it is only accessible through the functions present inside the class.

You experience encapsulation in your daily life all the time. Think about a human resources department. They encapsulate (hide) the information about employees. They determine how this data can be used and manipulated any request for the employee data or request to update the data has to be routed through them. Another examples network security. Any request for security information or a change to a security policy must be made through a network security administrator. The security data is encapsulated from the users of the network.

No comments

Post a Comment