class Car extends Automobile{ // Car has a Engine so, Car class has an instance of Engine class as its member.

what is the difference between dating and having a relationship-69what is the difference between dating and having a relationship-51what is the difference between dating and having a relationship-24

For example, 1) As clear in above code , Class Y has very strong coupling with class X.

If anything changes in superclass X , Y may break dramatically.

Suppose In future class X implements a method work with below signature Change is done in class X but it will make class Y uncompilable.

vector; you should not be allowed to insert and remove elements arbitrarily. Unfortunately it's too late to rectify this design mistake, since changing the inheritance hierarchy now would break compatibility with existing code.

I appreciate this answer; however, I feel as if the answer gets off track and delves more into concerns surrounding the design of the language (and a particular package) than it does answer the asked question regarding composition vs. I am a big fan of answering the question on SO, citing resources - not linking to external resources without providing a more in depth summary than a one-line summation.class Engine {} // The engine class.

class Automobile{} // Automobile class which is parent to Car class.

// Car is an Automobile, so Car class extends Automobile class.

So, from the car examples in the other questions, if I wanted to say that a car "has-a" gas tank, I would use composition, as follows: Inheritance brings out IS-A relation. Statergy pattern explains that Composition should be used in cases where there are families of algorithms defining a particular behaviour.

Classic example being of a duck class which implements a fly behaviour. You start off with a complete (or semi-complete) object and you replace or Override the various bits you want to change. With inheritance, some classes are completely implemented already, and others have methods that you are forced to override. (but you can Implement the interfaces by calling methods in other classes if you happen to have something laying around).