It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Why are we encouraged to override methods from Object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why are we encouraged to override methods from Object?" Watch "Why are we encouraged to override methods from Object?" New topic
Author

Why are we encouraged to override methods from Object?

James Elsey
Ranch Hand

Joined: Dec 21, 2007
Posts: 228

Its come up a few times in Head First Java, it mentions that we should ALWAYS override toString and equals.

I can't think of any reason why I would need to, what is wrong with using Object.equals? I can't see how I could override, and provide my own implementation better than what is already there :S


Kind Regards, James. OCPJP 1.6 || My SCJP / OCJCP Study Notes
Interested in : SCJP, Google App Engine, Stripes, Android;|| My Bite-Size SCJP Study Blog
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

Object.equals uses ==, and nothing else. Sometimes you want to override that. A good example is String. Imagine not being able to tell if two different String objects contain the same characters!

As for toString(), I really can't see much use in com.javaranch.Person@123456. I'd much rather prefer com.javaranch.Person[name=Rob,role=bartender,status=awesome]. It tells you much more.

Keep in mind that equals has a partner in crime - hashCode. Make sure to always follow the rules specified in the Javadoc of both methods if you override at least one of them.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
James Elsey
Ranch Hand

Joined: Dec 21, 2007
Posts: 228

Nice explanation, thanks Rob ;)
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

(I also include the default toString in my own implementations, as I often need to know if objects are the exact same instance, which I can't always tell from instance data.)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Rob Prime wrote: . . . com.javaranch.Person[name=Rob,role=bartender,status=awesome]. It tells you much more. . . .
Avishkar Nikale
Ranch Hand

Joined: Aug 06, 2010
Posts: 173
The plain Object methods need to be overridden for following reasons

1) toString() - For a string representation of your Class instance
2) equals() & hashCode() - For determining uniqueness of your Class instance
so that you can use them for comparisons, in collections etc
3) clone() - So that you can create replicas of your Class instance
or to prevent the same in some cases.
4) finalize() - to perform cleanup actions before the Class instance is garbage collected

You can study the java.lang.Object API
for detailed information.



Regards,
Avishkar Nikale
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Avishkar Nikale wrote: . . .
2) equals() & hashCode() - For determining uniqueness . . .
Not uniqueness, but identity. You can have two objects which have the same state, and they can return true from equals(). Remember to override both those method.
3) clone() - So that you can create replicas of your Class instance
or to prevent the same in some cases.
More commonly prevent. Remember you must implement the Cloneable interface, otherwise clone() won't work.
4) finalize() - to perform cleanup actions before the Class instance is garbage collected
That is rarely necessary: anybody posting questions on "beginning Java" will never need finalize().
You can study the java.lang.Object API
for detailed information.
Also, Google for Joshua Bloch's Effective Java™; there used to be a sample chapter from the 1st edition available free of charge on the net, which describes those methods.

There are also getClass(), notify(), notifyAll() and three wait() methods which you don't usually override. In fact, I think they are final, so you can't override them.
Avishkar Nikale
Ranch Hand

Joined: Aug 06, 2010
Posts: 173
Thanks Campbell for detailing my summary & correcting where required.

James,

We hope to have at least lead you to a path of understanding why to override Object methods.




Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Avishkar Nikale wrote:Thanks . . .
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

Campbell Ritchie wrote:There are also getClass(), notify(), notifyAll() and three wait() methods which you don't usually override. In fact, I think they are final, so you can't override them.

They are indeed final.
Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

Very nice explanation. Got things cleared in my mind also . Thank you all .


Understanding is Everything - Peter Lord
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why are we encouraged to override methods from Object?