title: Difference between dependency and composition?
body: <p><a href="http://javapapers.com/oops/association-aggregation-composition-abstraction-generalization-realization-dependency/" rel="noreferrer">Definitions taken from here</a></p>
  
  <p>Dependency</p>
  
  <blockquote>
    <p>Change in structure or behaviour of a class affects the other related
    class, then there is a dependency between those two classes. It need
    not be the same vice-versa. When one class <strong>contains the other</strong> class it
    this happens.</p>
  </blockquote>
  
  <p>Composition</p>
  
  <blockquote>
    <p>Composition is a special case of aggregation. In a more specific
    manner, a restricted aggregation is called composition. When an object
    <strong>contains the other</strong> object, if the contained object cannot exist
    without the existence of container object, then it is called
    composition.</p>
  </blockquote>
  
  <p>Concrete examples in Java from <a href="https://stackoverflow.com/questions/11881552/implementation-difference-between-aggregation-and-composition-in-java/12431359#12431359">here</a> and <a href="https://stackoverflow.com/questions/1638919/how-to-explain-dependency-injection-to-a-5-year-old">here</a></p>
  
  <p>Dependency</p>
  
  <pre><code>class Employee {
      private Address address;
  
      // constructor 
      public Employee( Address newAddress ) {
          this.address = newAddress;
      }
  
      public Address getAddress() {
      return this.address;
      }
      public void setAddress( Address newAddress ) {
          this.address = newAddress;
      }
  }
  </code></pre>
  
  <p>Composition</p>
  
  <pre><code>final class Car {
  
    private final Engine engine;
  
    Car(EngineSpecs specs) {
      engine = new Engine(specs);
    }
  
    void move() {
      engine.work();
    }
  }
  </code></pre>