Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have gone through lots of theories about what is encapsulation and the three techniques of implementing it, which are Association, Aggregation and Composition.

What I found is:


Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding.

Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Access to the data and code is tightly controlled by an interface.

The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code.


Association is a relationship where all object have their own lifecycle and there is no owner. Let’s take an example of Teacher and Student. Multiple students can associate with a single teacher and a single student can associate with multiple teachers, but there is no ownership between the objects and both have their own lifecycle. Both can create and delete independently.


Aggregation is a specialized form of Association where all objects have their own lifecycle, but there is ownership and a child object cannot belong to another parent object. Let’s take an example of a Department and teacher. A single teacher cannot belong to multiple departments, but if we delete the department the teacher object will not be destroyed. We can think of it as a “has-a” relationship.


Composition is again a specialized form of Aggregation and we can call this as a “death” relationship. It is a strong type of Aggregation. Child object does not have their lifecycle and if parent object deletes all child object will also be deleted. Let’s take again an example of relationship between House and rooms. House can contain multiple rooms but there is no independent life of a room and any room cannot belong to two different houses. If we delete the house, the room will automatically be deleted.

The question is:

Now these all are real world examples. I am looking for some description about how to use these techniques in actual class code. I mean what is the point for using three different techniques for encapsulation, How these techniques could be implemented and How to choose which technique is applicable at time.

share|improve this question
Please note that Aggregation, Composition and Association are not techniques to implement encapsulation. An encapsulation can also exist even if there is just one class/object. Encapsulation is just an essential feature to in Object Orientation to hide your object's data. – Maxood Nov 22 '13 at 21:24
"A single teacher can NOT belongs to multiple departments" really? Didn't you notice your resource modified the content ? – KNU Feb 12 '15 at 8:04
I've managed to use Composition to identify DDD Aggregate Roots and their Entities when doing Domain-Driven Design. A new use for an old convention. – Jonn Mar 14 at 1:59
up vote 8 down vote accepted

The distinction between association, aggregation and composition as you describe it is a legacy going back to the old times of manual memory management. For example in C++ the memory used by objects has to be manually released and it is thus paramount to carefully design the lifecycle of composed objects. While the distinction between aggregation and composition is still being taught by many text books, it is essentially irrelevant when programming in environments with automatic memory management. If you have garbage collection all of them are just composition, period.

Encapsulation on the other hand is a much more general principle than what you describe. It is foremost the idea of bundling data and the functions that operate on this data in one module. One way to implement this is by keeping the state of the module private and expose changes to that state through public services. So client cannot access the state on their own but have to tell the module their intend by sending messages. So encapsulation is not limited to objects but applies to services as well. Actually, one way of looking at objects is to look at them as services.

Here's an example of encapsulation

public class Counter {
    private int n = 0;
    public int inc() { return n++; }

or the same using lambda functions

var counter = (function() {
    var n = 0;
    var inc = function() { return n++; }
    return inc;

In both cases the data, that is the variable n, is bundled together with the function inc that operates on it. And there is no way any other function could ever access n, thus we have an encapsulated module that provides counting as a service.

NB: exposing all of an object's inner state through accessors is actually a violation of encapsulation. Alas, it is such a common violation that many will confuse it with good object-oriented design.

share|improve this answer
Automatic Memory Management has nothing to do with object relationships. An object diagram itself tells the programmer how to code the classes, their interfaces and their methods as per the requirements of the system under consideration. – Maxood Nov 22 '13 at 21:21
There's also a consideration outside of memory. In data design or perhaps serialization. In composition, you may need to have different foreign key constraints in a RDBMS, or possibly change how you handle serialization of an object if it has composite children versus aggregate children or associated objects. – Chris Jun 15 '15 at 17:17

Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding.

    public class Test{

    private String name;

       private int age;

       public int getAge(){
          return age;

       public String getName(){
          return name;

Refer this question also.

Association indicates the relationship between objects. eg: Computer uses keyboard as input device.

An association is used when one object wants another object to perform a service for it.

Aggregation is a special case of association. A directional association between objects. When an object ‘has-a’ another object, then you have got an aggregation between them.

eg: Room has a table, but the table can exist without the room.

    class Room {

      private Table table;

      void setTable(Table table) {
        this.table = table;


Composition is a special case of aggregation. Composition is more restrictive. When there is a composition between two objects, the composed object cannot exist without the other object. This restriction is not there in aggregation. eg: rooms in a house, which cannot exist after the lifetime of the house.

    class House {

      private  Room room;

      House(Room roomSpecs) {
        room = new Room(roomSpecs);


Composition is a design technique to implement has-a relationship in classes, either by Inheritance or object Composition for code reuse.

One of the best practice in Java programming is to use composition over inheritance

share|improve this answer
how does this answer the question asked? – gnat Nov 22 '13 at 10:32
gnat now this is corrected thanks – kapil das Nov 26 '13 at 5:35

Using those techniques usually results in design practices like SOLID or various design patterns.

The point of using patterns, practices and such is to describe a solution to a specific problem that is also maintainable and extensible. You simply need to get enough experience to tell where to use which pattern or technique.

share|improve this answer

I frankly feel that these notions being taught at academic do have their importance in the contexts of object orientation and class design. These concepts aid us a lot when it comes to modelling a system from the scratch. Association, Aggregation and Composition belong exclusively to UML's class diagram and are completely independent of technology constraints like memory issues.

Furthermore, you also have to consider the higher level or business goals of the system you are modelling. We have objects like House and Room in our system under consideration, but cannot be strongly related (via composition). For instance, if I am modelling a real estate system then I might have to know what room belongs to what house. But lets that I am modelling a surveying or census system where i want to know how many people are living in each room of the house in a certain area, then I just do not need to relate a room with a house via composition.

Another example could be of an orchard and certain kind of fruit. Lets say I can only consider an orchard when I have apple trees planted inside it. The bottom line is that the requirements of the overall system does matter a lot.

Encapsulation is one of the pillars of Object Oriented Design. You need to bundle your data and the operations you will perform on your data. also you have to hide certain attributes of your object from the outer world in order to enable that object to survive in a valid state. When 2 objects are interacting, they have to interact each other via an interface. And that's what encapsulation ensures about when we design our OO system.

Here is how these concepts are applied to the code:

ASSOCIATION: Association indicates the relationship between objects. It lets the programmer know what methods to write in their classes to make them interact with each other. You can find several examples of code and class diagrams to understand association. In your example of Teach and Student, there is a relationship of teaching and taught by. So you will simply write a set of methods (technically called interface) via which you can get to know which student has what teachers and which teacher has what students. Association also enables the system modeller to aid the database designer about the attributes and fields that need to be kept in the database.

COMPOSITION: If one object is an integral part of another object, then i might have to indicate this relationship in the constructor of the other object. For example, in your scenario of Houses and Rooms we can write the following code in case we want to know which room belongs to what house type.

class House{
          string _HouseType;   
    void setHouseType(string house_type)
        this. _HouseType = house_type;

     string getHouseType()
       return _HouseType;

 House HouseObject = new House();

class Room{

 Room(string HouseType) {
       this._HouseType = HouseObject.getHouseType();  //as in my system a room cannot exist without a house



The programmer will also ensure while invoking destructors of the object, that the other object's destuctor also gets invoked. This is crucial.

AGGREGATION: Lets say if the relationship between objects is weak then we for the programmer, it would mean to use an instance variable instead to indicate the relationship. And then write a mutator functon (setter) to provide value to that object from another one.

class Department{

 string dept_name;

   void setDeptName(string name)

   string getDeptName()
        return dept_name; 


 Department DepartmentObject = new Department();

class Teacher{

 string dept_name;


  setDeptName(string name)
     this.dept_name = DepartmentObject.getDeptName();  //You only need to invoje this method when needed (aggregation)

share|improve this answer

Your Answer


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.