views:

6771

answers:

8

Yesterday I had a two-hour technical phone interview (which I passed, woohoo!), but I completely muffed up the following question regarding dynamic binding in Java. And it's doubly puzzling because I use to teach this concept to undergraduates when I was a TA a few years ago, so the prospect that I gave them misinformation is a little disturbing...

Here's the problem I was given:

/* What is the output of the following program? */

public class Test {

  public boolean equals( Test other ) {
    System.out.println( "Inside of Test.equals" );
    return false;
  }

  public static void main( String [] args ) {
    Object t1 = new Test();
    Object t2 = new Test();
    Test t3 = new Test();
    Object o1 = new Object();

    int count = 0;
    System.out.println( count++ );// prints 0
    t1.equals( t2 ) ;
    System.out.println( count++ );// prints 1
    t1.equals( t3 );
    System.out.println( count++ );// prints 2
    t3.equals( o1 );
    System.out.println( count++ );// prints 3
    t3.equals(t3);
    System.out.println( count++ );// prints 4
    t3.equals(t2);
  }
}

I asserted that the output should have been two separate print statements from within the overridden equals() method: at t1.equals(t3) and t3.equals(t3). The latter case is obvious enough, and with the former case, even though t1 has a reference of type Object, it is instantiated as type Test, so dynamic binding should call the overridden form of the method.

Apparently not. My interviewer encouraged me to run the program myself, and lo and behold, there was only a single output from the overridden method: at the line t3.equals(t3).

My question then is, why? As I mentioned already, even though t1 is a reference of type Object (so static binding would invoke Object's equals() method), dynamic binding should take care of invoking the most specific version of the method based on the instantiated type of the reference. What am I missing?

+3  A: 

I think the key lies in the fact that the equals() method doesn't conform to standard: It takes in another Test object, not Object object and thus isn't overriding the equals() method. This means you actually have only overloaded it to do something special when it's given Test object while giving it Object object calls Object.equals(Object o). Looking that code through any IDE should show you two equals() methods for Test.

P Arrayah
This, and the most of the responses are missing the point. The issue is not about the fact that overloading is being used instead of overriding. It is why isn't the overloaded method used for t1.equals(t3), when t1 is declared as Object but initialized to Test.
Robin
+1  A: 

The method is overloaded instead of overriden. Equals always take an Object as parameter.

btw, you have an item on this in Bloch's effective java (that you should own).

Gilles
Joshua Bloch's Effective Java?
DJClayworth
Effective yeah, was thinking of something else while typing :D
Gilles
+6  A: 

The equals method of Test does not override the equals method of java.lang.Object. Look at the parameter type! The Test class is overloading equals with a method that accepts a Test.

If the equals method is intended to override, it should use the @Override annotation. This would cause a compilation error to point out this common mistake.

erickson
Yeah, I'm not quite sure why I missed that simple yet crucial detail, but that's exactly where my problem was. Thank you!
Magsol
+1 for being the true answer to the questioner's curious results
matt b
+3  A: 

Java does not support co-variance in parameters, only in return types.

In other words, while your return type in an overriding method may be a subtype of what it was in the overridden, that is not true for parameters.

If your parameter for equals in Object is Object, putting an equals with anything else in a subclass will be an overloaded, not an overridden method. Hence, the only situation where that method will be called is when the static type of the parameter is Test, as in the case of T3.

Good luck with the job interview process! I'd love to be interviewed at a company that asks these types of questions instead of the usual algo/data structures questions that I teach my students.

Uri
You mean contravariant parameters.
Tom Hawtin - tackline
I somehow completely glossed over the fact that different method parameters intrinsically create an overloaded method, not an overridden one.Oh don't worry, there were algo/data structures questions as well. :P And thank you for the good luck, I'll need it! :)
Magsol
+3  A: 

Interestingly enough, in Groovy code (which could be compiled to a class file), all but one of the calls would execute the print statement. (The one comparing a Test to an Object clearly won't call the Test.equals(Test) function.) This is because groovy DOES do completely dynamic typing. This is particularly of interest because it does not have any variables that are explicitly dynamically typed. I have read in a couple of places that this is considered harmful, as programmers expect groovy to do the java thing.

Benson
Unfortunately the price that Groovy pays for that is a massive performance hit, as every method invocation uses reflection.Expecting one language to work exactly the same as another one is generally considered harmful. One needs to be aware of differences.
Joachim Sauer
Should be nice and fast with invokedynamic in JDK7 (or even using similar implementation technique today).
Tom Hawtin - tackline
A: 

See also this SO Question, closely related: Overriding the JAVA equals method quirk

Ken Gentle
+16  A: 

Java uses static binding for overloaded methods, and dynamic binding for overridden ones. In your example, the equals method is overloaded (has a different param type than Object.equals()), so the method called is bound to the reference type at compile time.

Some discussion here

The fact that it is the equals method is not really relevant, other than it is a common mistake to overload instead of override it, which you are already aware of based on your answer to the problem in the interview.

Edit: A good description here as well. This example is showing a similar problem related to the parameter type instead, but caused by the same issue.

I believe if the binding were actually dynamic, then any case where the caller and the parameter were an instance of Test would result in the overridden method being called. So t3.equals(o1) would be the only case that would not print.

Robin
A lot of people point out that it's overloaded and not overridden, but even with that you'd expect it to resolve the overloaded one correctly. Your post is actually the only one so far that answers the question correctly as far as I can tell.
Bill K
My mistake was completely missing the fact that the method is indeed overloaded rather than overridden. I saw "equals()" and immediately thought inherited-and-overridden. Looks like I, yet again, got the broader and more difficult concept correct, but screwed up the simple details. :P
Magsol
A: 

The answer to the question "why?" is that's how the Java language is defined.

To quote the Wikipedia article on Covariance and Contravariance:

Return type covariance is implemented in the Java programming language version J2SE 5.0. Parameter types have to be exactly the same (invariant) for method overriding, otherwise the method is overloaded with a parallel definition instead.

Other languages are different.

ykaganovich
My problem was roughly equivalent to seeing 3+3 and writing 9, then seeing 1+1 and writing 2. I understand how the Java language is defined; in this case, for whatever reason, I completely mistook the method for something it wasn't, even though I avoided that mistake elsewhere in the same problem.
Magsol