Choosing composition vs. inheritance
by, 04-26-2012 at 06:14 PM (796 Views)
Both inheritance and composition permits you to put the sub-objects in new class. One may be amazed regarding the difference present b/w two and when one could be chooses over the other.
Generally, composition is used if an existing class features are required in your new class but not the interface. This means that an object is embedded to use it for implementation of functionality of your new class. However, your new class user may see the interface that has been defined instead of the interface, from embedded object. Private objets are embedded of the existing classes that are present in your new class.
Sometimes, class users shall be allowed to access your new class composition directly which means to public the object member. Implementation hiding is used by the member objects themselves hence this is considerably a safer thing. When bunch of parts are being assembled, this will be an easier task for interfaces to be understood. Good example is a car object: