• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

public boolean equals (Object obj)

 
Mahi Rahman
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!
I have an interesting problem with the following code. Whenever I try to run this program it gives me false(No!) for (date.equals(date2)) method even though it should return true(Yes!). I tried to figure out the fault using debugger but could not succeded. Can anyone please help me?

Mahi.


EDIT by mw: Added Code Tags.
[ February 16, 2007: Message edited by: marc weber ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

Your date is a reference of type Date. But your date1, date2, and date3 are all of type Object.

You've overloaded the equals method to take a Date object, but when you call it using an Object as an argument, the equals method inherited from Object is invoked. (Remember, calls to overloaded methods are resolved based on the arguments' reference types -- not their true runtime types. This is different than overridden method calls, which are resolved based on the calling object's true runtime type.)
[ February 16, 2007: Message edited by: marc weber ]
 
Mahi Rahman
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marc!
I now understand what was the flaw in my code. Also thanks for editing with [CODE] tags. Cheers!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic