File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes public boolean equals (Object obj) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "public boolean equals (Object obj) " Watch "public boolean equals (Object obj) " New topic

public boolean equals (Object obj)

Mahi Rahman

Joined: Feb 16, 2007
Posts: 21
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?


EDIT by mw: Added Code Tags.
[ February 16, 2007: Message edited by: marc weber ]
marc weber

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Mahi Rahman

Joined: Feb 16, 2007
Posts: 21
Thanks Marc!
I now understand what was the flaw in my code. Also thanks for editing with [CODE] tags. Cheers!
I agree. Here's the link:
subject: public boolean equals (Object obj)
It's not a secret anymore!