title: Overriding equals() method in a generic class
body: <p>I want to override equals() method in this class. I follow the usual rules while overriding the equals() method but and I typecast the Object into my class type</p>
  
  <p>but in my equals() method, I want to return true only if the objects are of the same generic type. </p>
  
  <p>How can I check the run time type of the instance in my equals() method?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public class GenericsRunTimeType&lt;T&gt; {
  
  private T foo;
  public GenericsRunTimeType(T foo){
      this.foo = foo;
  
  }
  
  @Override
  public boolean equals(Object obj){
      if (this == obj)
          return true;
      if (obj == null)
          return false;
      if (getClass() != obj.getClass())
          return false;
  
      // Before doing this I want to test if obj and this are of the same generic type
      GenericsRunTimeType other = (GenericsRunTimeType) obj;
      if(other.equals(obj))
          return true;
      else
          return false;
  }
  </code></pre>
  
  <p>}</p>