Function Composition or Object Composition
Composition is the technique of building complex objects by combining simpler objects, rather than inheriting from a base class.
Object Composition in Java
Composition in java is the design technique to implement has-a relationship in classes.
Java composition is achieved by using instance variables that refers to other objects. For example, a Person has a Job.
It is done by declaring an instance variable referencing the object of the existing class. Then, by initializing the object either through the constructor or a method, and finally using the referred (composed) object.
Object composition is defined at runtime through objects acquiring references to other objects. In such a case, these objects will never be able to reach each-other’s protected data (no encapsulation break) and will be forced to respect each other’s interface. Implementation dependencies will be a lot less than in case of inheritance. This is one of the advantages of Composition over Inheritance.