title: Using java generics to ensure that argument received is same as class or subtype thereof
body: <p>I have a newbie question.</p>
  
  <pre><code>interface Animal {
      void partner(Animal other);
  }
  
  class Lion implements Animal {
      int areaUnderControl;
      @Override
      public void partner(Animal other) {
          Lion lion = (Lion) other;
          this.areaUnderControl += lion.areaUnderControl; 
      }
  }
  
  class Human implements Animal {
      int money;
      @Override
      public void partner(Animal other) {
          Human human = (Human) other;
          this.money += human.money;
      }
  }
  </code></pre>
  
  <p>I want that the partner method in the concrete implementations only receive parameters of its type or its subtype. One solution is to use generics to define Animal as:</p>
  
  <pre><code>interface Animal&lt;T extends Animal&lt;T&gt;&gt; {
      void partner(T other);
  }
  
  class Lion implements Animal&lt;Lion&gt; {
      int areaUnderControl;
      @Override
      public void partner(Lion other) {
          this.areaUnderControl += other.areaUnderControl; 
      }
  }
  
  class Human implements Animal&lt;Human&gt; {
      int money;
      @Override
      public void partner(Human other) {
          this.money += other.money;
      }
  }
  </code></pre>
  
  <p>However someone can still abuse this as</p>
  
  <pre><code>class Bear implements Animal&lt;Lion&gt; {
      int honey;
      @Override
      public void partner(Lion other) {
          this.honey += other.areaUnderControl; //Problem
      }
  }
  </code></pre>
  
  <p>where a Bear is aspiring to partner with Lion. Is there someway that I can tie it to the type and subtypes of <em>this</em></p>
  
  <p>I tried searching through the existing questions and somehow I feel the design of the interface itself may be flawed. If someone can point out why this is wrong or the right way to achieve this, it would be greatly appreciated.</p>
  
  <p>Thanks!</p>