title: What is the difference between using generics and interfaces in methods and classes
body: <p>I'm wondering what is the difference between using an interface (or a supertype) in a generic class or method and using the generic approach with bounding (T extends Interface).</p>
  
  <p>Let's say that I have two classes that implement and interface (naive examples):</p>
  
  <pre><code>public interface MeasurableDistance {
      public Point getPosition();
  }
  
  public class Person implements MeasurableDistance {
      private Point position;
  
      public Point getPosition() {
          return position;
      }
  }
  
  public class House implements MeasurableDistance {
      private Point position;
  
      public Point getPosition() {
          return position;
      }
  }
  </code></pre>
  
  <p>1) What would be the difference between writing a method in the following ways:</p>
  
  <pre><code>public int computeDistance(MeasurableDistance a, MeasurableDistance b) {
      Point a = a.getPosition();
      Point b = b.getPosition();
      //compute distance
      return distance,
  }
  </code></pre>
  
  <p>and something like</p>
  
  <pre><code>public &lt;T extends MeasurableDistance, S extends MeasurableDistance&gt; int computeDistance(T a, S b) {
      Point a = a.getPosition();
      Point b = b.getPosition();
      //compute distance
      return distance,
  }
  </code></pre>
  
  <p>2) And if I wanted a class to hold a MeasurableDistance object:</p>
  
  <pre><code>public class Holder {
      private MeasurableDistance holder;
  
      public Holder() {};
      public add(MeasurableDistance a) {
          holder = a;
      }
  }
  </code></pre>
  
  <p>or something like</p>
  
  <pre><code>public class Holder&lt;T extends MeasurableDistance&gt; {
      private T holder;
  
      public Holder&lt;T&gt;() {};
      public add(T a) {
          holder = a;
      };
  }
  </code></pre>
  
  <p>For 1, I think they should be pretty much identical, right? Obviously calling the non generic version</p>
  
  <pre><code>Person a = new Person();
  Person b = new Person();
  House c = new House();
  House d = new House();
  computeDistance(a,c);
  computeDistance(a,b);
  computeDistance(c,d);
  </code></pre>
  
  <p>would always work because computeDistance() would be seeing those objects as MeasurableDistance because of polymorphism. The generic version instead should work too right? The compiler would infer the MeasurableDistance type and add casts accordingly. Even if I wanted to call it this way:</p>
  
  <pre><code>&lt;Person, House&gt;computeDistance(a,c);
  </code></pre>
  
  <p>it would have the same effect, the method would look like</p>
  
  <pre><code>    Point a = (MeasurableDistance) a.getPosition();
      Point b = (MeasurableDistance) b.getPosition();
  </code></pre>
  
  <p>if I'm not mistaken. Or, now that I think about it, it should look like this:</p>
  
  <pre><code>    Point a = (Person) a.getPosition();
      Point b = (House) b.getPosition();
  </code></pre>
  
  <p>and it would work because both Person and House are implementing that method.</p>
  
  <p>So what would be the advantage of the generic method? I've read that it's typesafe because casts are always right, but with the non generic method you won't have casts at all.</p>