Henry Wong wrote:Hint: Are you sure that you are calling your equals() method? Or the one the you inherited from the Object class?
[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Jeanne Boyarsky wrote: Or you can see for yourself by renaming your method from "equals" to "equalsValue".
Jeanne Boyarsky wrote:Tiffany,
Welcome to JavaRanch!
Take a look at your equals() method. What does it take as a parameter? What are you passing? (it isn't the same type.)
If you don't know the answer to either of these questions, post which one you do know. Or you can see for yourself by renaming your method from "equals" to "equalsValue". This will give you a compiler error rather than functioning in a way to don't expect. And the compiler will answer the questions too.
Tiffany Smith wrote:I think both counter and counter2 are of the Counter class... I'm not getting an incompatible error with the code as it's written right now. Are they string values? I'm totally confused. Sorry to be so totally dense...
Henry Wong wrote:
Tiffany Smith wrote:I think both counter and counter2 are of the Counter class... I'm not getting an incompatible error with the code as it's written right now. Are they string values? I'm totally confused. Sorry to be so totally dense...
Just because you are not getting a compiler error doesn't mean that it is correct.
Your equals() method takes an primative int. You are passing it a Counter object. Obviously, these two types are completely incompatible... Something is obviously wrong.
What is wrong is... it is not calling your equals() method, but is calling the equals() method that is inherited from the Object class.... as already mentioned.
Henry
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Prime wrote:Does your class have an equals method with exactly the same parameter types as Object.equals? If not then you're not overriding that method but overloading it instead.
Note that an equals method should not throw any exceptions if null is passed, or if the argument is of a completely different class. It should return if that is the case.
Bert Wilkinson wrote:From stuck on homework to "dropped the class" in less than 4 hours. Whew, what a trooper!
Johnny Doe wrote:Hi all, I'm reviving this thread because I'm stuck at the same point - same point of not understanding. And same homework problem. I understand that I can create a method an invoke it by using the object then a dot then the method. I don't get this equal overiding thing.
I think the book must bury this info somewhere.
Anyone want to demonstrate it (not the homework but rather the idea) with a few lines of code I can run (but not too few)? Maybe comment it? Thanks.
And you have only 4 hours or else..... :}
Johnny Doe wrote:I guess, that when I do that- it works. I didn't really know that I could send a Counter object into the method.
But I guess that is what alerts the compiler to do an override (?)
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |