title: Generic classes in java and equals() method: type safety
body: <p>I want to override equals() method in generic class and to do that I must cast Object to my generic type Pair.</p>
  
  <p>I added @SuppressWarnings("unchecked") to "mute" the warnings, but the problem is still there. Methods getType() and getClass() aren't working with generic types too, so using T.getType() is out of the question.</p>
  
  <pre><code>public class Pair&lt;T, U&gt; {
      private T first;
      private U second;
  
      public Pair(T _first, U _second)
      {
          first = _first;
          second = _second;
      }
  
      public boolean equals(Object obj) {
           if (this == obj)
             return true;
           if (obj == null)
             return false;
           if (getClass() != obj.getClass())
             return false;
  
          //Problem ahead!!!
           Pair&lt;T, U&gt; other = (Pair&lt;T, U&gt;) obj;
  
                     ...
      }
  }
  </code></pre>
  
  <p>Is there any general good practice or "trick" to do that correctly and safely?</p>