Sunday, March 15, 2009

Puzzle 6 - Solution

The key to the puzzle is ‘Equals Method has been overridden not overloaded’.

The equals method in the Object class and hence the Base class has the signature public boolean equals(Object o).

When we call child.equals(base) then the equals method in the Child class gets executed since it has a overloaded equals method with signature public boolean equals(Base b).

However when we call base.equals(child), even though base points to a Child Object compile time bindings ensure that the Base version of the equals method namely equals(Object o) gets called.
The output that is printed by this program is Child Equals Base

Sorry no correct answers this week!

2 comments:

  1. >> The output that is printed by this program is Child Equals Base

    Wrong!

    Base eqals Child! is the correct answer

    ReplyDelete
  2. Yep Leonid - your right - I got a typo in the post there - the program should print (& does) 'Base equals Child!'

    ReplyDelete

Solution for this question?