• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

equals override

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A "commercial" mock test gives the following override for method equals:

The answer says this is the "right" solution.
There are however 2 problems with this code:
1) It does not compile.
This is because it cannot override the actual Object.equals which is:
public boolean equals(Object obj)
2) There is a problem with the return statement
What this override does is: nothing at all !
This is what Object.equals() already does: if both references are equal return true.
However the purpose of overriding equlas is to compare 2 objects meaningfully, not based on the reference.
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi John
Which A "commercial" mock test gives the following override for method equals?

May be a MC?
please do not write the whole name
I think a part of name may be acceptable
 
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe he is talking about our exam simulator, JQPlus.
John, the bug that you reported about the return value was fixed immediately after you reported it last week.
Regarding this particular case and your other post, I replied to you saying that they are perfectly valid implementations of the equals method.


This is what Object.equals() already does: if both references are equal return true.
However the purpose of overriding equlas is to compare 2 objects meaningfully, not based on the reference.


As an exam candidate, it is not your concern whether the equals implementation given to you makes sense logically or not or even whether it is useful or not. Your concern is whether it is technically correct or not...whether it will compile and whether it adheres to the rules given by the JLS. And both of them do. They do not break any rule and so they are correct choices.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Anil:
As an exam candidate, it is not your concern whether the equals implementation given to you makes sense logically or not or even whether it is useful or not. Your concern is whether it is technically correct or not...whether it will compile and whether it adheres to the rules given by the JLS.


Is that true? Who can verify?
I am using jqplus for a month and it looks good. I was supposed to take exam last month but changed date because I am not scoring well in jqplus. scoring only 50%.
thank you.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ho Gong:
Is that true? Who can verify?


I always try to stress the fact that the SCJP exam really emphasizes knowledge of the Java syntax - not really knowledge of good programming techniques. For that, I'd recommend that you go on to the developer's certification.
In this case (except for the return value), the implementation given for the equals method is perfectly legitimate. Of course, this method is also legitimate:

That's legal code - it will compile (or should, at least - I didn't test it) and run. Of course, it probably won't do what you'd expect a equals method to do, but there's no reason you couldn't use that as an equals method - it follows all the syntax rules of Java.
When you take the exam, you don't need to concern yourself if the code is "good" code, you really only need to concern yourself if the code will work.
I hope that helps,
Corey
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Corey, your example is illegal according to the API:
The equals method implements an equivalence relation:
It is reflexive: for any reference value x, x.equals(x) should return true.
It is symmetric: for any reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
It is transitive: for any reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
It is consistent: for any reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the object is modified.
For any non-null reference value x, x.equals(null) should return false.

So an equals that always returned true would be invalid but an equals that always returns false is valid.
 
John Zoetebier
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thomas says:


So an equals that always returned true would be invalid but an equals that always returns false is valid.


If an equals override always returns false, then
(x.equals(x) == false) will be true.
This violates the reflexive rule of equality.
Stated differently: an equals method cannot always return false.
See also my posting of "equals override: 2"
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct. This would be an acceptable equals method:

This would also be acceptable:

[ April 22, 2003: Message edited by: Thomas Paul ]
reply
    Bookmark Topic Watch Topic
  • New Topic