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.
The three links Association, Aggregation and Composition form a kind of scale on how closely two classes are related to each other.
On the one end of the scale, there is Association, where objects of the two classes can know about each other, but they do not affect each others lifetime. The objects can exist independently and which class A object knows about which class B objects can vary over time.
On the other end of the scale, there is Composition. Composition represents a part -- whole relationship such that class B is an integral part of class A. This relationship is typically used if objects of class A can't logically exist without having a class B object.
The Aggregation relation is somewhere between those two ends, but nobody seems to agree where exactly, so there is also no universally agreed definition of what an Aggregation means. In that sense, both definitions that you found are correct and if you ask 10 people, you risk getting 11 different definitions.
Best Answer
Yes they are, composition is usually called a "strong" association, because if you get rid of the main class the other class has no reason for existance. Aggregation is a sort of "light" association, in which the deletion of one of the related classes does not interfere in the existance of the other.