title: When overriding equals in Java, why does it not work to use a parameter other than Object?
body: <p>I ran into an interesting behavior recently.  It seems that if I override .equals() to take a parameter other than Object, it doesn't get called.  Can anyone explain to me why this is happening?  It seems to violate my understanding of polymorphism in OOP, but maybe I'm missing something.</p>
  
  <p>Here's much simpler code that shows what I'm seeing:</p>
  
  <pre><code>public class MyClass {
    private int x;
    public MyClass(int n) { x = n; }
    public boolean equals(Object o) { return false; }
    public boolean equals(MyClass mc) { return x == mc.x; }
    public static void main(String[] args) {
      List&lt;MyClass&gt; list = new ArrayList&lt;MyClass&gt;();
      list.add(new MyClass(3));
      System.out.println("Contains 3? " + list.contains(new MyClass(3)));
    }
  }
  </code></pre>
  
  <p>When this is run, it prints "<code>Contains 3? false</code>".  It looks like the equals(Object) function is called, even though there is another that would work.  By contrast, if I write equals like this the code works as expected:</p>
  
  <pre><code>public boolean equals(Object o) {
    if(!(o instanceof MyClass))
      return false;
    MyClass mc = (MyClass)o;
    return x == mc.x;
  }
  </code></pre>
  
  <p>Why isn't it figuring out which version of the function to call based on the type of the parameter?</p>