This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Override Equals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Override Equals" Watch "Override Equals" New topic
Author

Override Equals

Singh Harmeet
Ranch Hand

Joined: Aug 05, 2011
Posts: 115



When we try to run this code, there is 2 compile time errors generate

TestOne.java:13: error: cannot find symbol
if((o instanceof TestOne ) && ( (TestOne)o.getTestValue() == thi
s.getTestValue() )){
^
symbol: method getTestValue()
location: variable o of type Object
TestOne.java:13: error: incomparable types: TestOne and int
if((o instanceof TestOne ) && ( (TestOne)o.getTestValue() == thi
s.getTestValue() )){
^
2 errors


With Regards :-
Harmeet Singh
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3605
    
  14

I assume you're wondering why you're getting an error?

The dereference (dot) operator has higher precedence than the cast operator does. This means the compiler thinks you're trying to call getTestValue() before you're casting o to TestOne.

Aside from that, you shouldn't have statements of the form
Instead, do
Note your class does not conform to Object's contract. You're not overriding the hashCode() method in accordance with the equals() method.
Singh Harmeet
Ranch Hand

Joined: Aug 05, 2011
Posts: 115

thanks mr. Stephan van Hulst , now my program is compiled successfully.......
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Override Equals
 
Similar Threads
instanceof
Some fun while Overriding equals
Doubt about instanceof operator
Generics and collections K&B pg 526
Accessing private variables in equals()