The Artima Developer Community
Sponsored Link

Java Answers Forum
overriding methods in java.lang.object

6 replies on 1 page. Most recent reply: Oct 6, 2003 10:51 PM by sandesh

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
sandesh

Posts: 10
Nickname: sandesh
Registered: Sep, 2003

overriding methods in java.lang.object Posted: Sep 30, 2003 10:15 PM
Reply to this message Reply
Advertisement
hi,
i have an hierarchy of java classes..it looks like this

person-->student
--> employee--->classified
--->unclassified

(if u didn't get the above dia..its like student and employee extends person and so on)

My question is when we r overiding the Object methods like clone,equals and hashcode..is it enough if we override these methods in Person class?? or do we have to do it in all the subclasses..


sandesh

Posts: 10
Nickname: sandesh
Registered: Sep, 2003

Re: overriding methods in java.lang.object Posted: Sep 30, 2003 10:17 PM
Reply to this message Reply
actually the diagram doesn't look very explanatory..so

student and employee extend Person class
classifed and unclassified extend Employee..

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: overriding methods in java.lang.object Posted: Oct 1, 2003 1:29 AM
Reply to this message Reply
First of all is there any real requirement to override these methods?

Becuase you haven't overiden in the sublcasses are you facing any problem (like in classes like HashTable or HashMap???)

sandesh

Posts: 10
Nickname: sandesh
Registered: Sep, 2003

Re: overriding methods in java.lang.object Posted: Oct 1, 2003 8:48 AM
Reply to this message Reply
> First of all is there any real requirement to override
> these methods?
>
> Becuase you haven't overiden in the sublcasses are you
> facing any problem (like in classes like HashTable or
> HashMap???)

hi,
The problem description is something like this..
since we have to use the keys in the hashmap as the objects themselves, we should override the equals and hashcode methods..and we have to do a directory search given the first and last names.

Adam Davis

Posts: 2
Nickname: adavis
Registered: Sep, 2003

Re: overriding methods in java.lang.object Posted: Oct 1, 2003 10:34 AM
Reply to this message Reply
> > First of all is there any real requirement to override
> > these methods?
...snip...
> The problem description is something like this..
> since we have to use the keys in the hashmap as the
> objects themselves, we should override the equals and
> hashcode methods..and we have to do a directory search
> given the first and last names.

Here is an excerpt of what I have done in the past. I'm not an expert, so I welcome any comments on the pros and cons of this approach.

public class DailyNotes implements Cloneable, Comparable, Serializable {
protected Date day;
protected String notes;

...

/* Interface methods */

public int compareTo(Object o) throws ClassCastException {
if (o instanceof DailyNotes) {
int res= this.day.compareTo( ((DailyNotes)o).getDay() );
if (res == 0) {
return( this.notes.compareTo( ((DailyNotes)o).getNotes()) );
}
else {
return(res);
}
}
else {
throw new ClassCastException("Error: Could not compare an object of Class "
+ o.getClass().getName() + " to a DailyNotes object");
}
}

/* Overridden methods */

public boolean equals(Object o) {
if (o instanceof DailyNotes) {
return( this.compareTo((DailyNotes)o) == 0 );
}
else {
return(false);
}
}

public int hashCode() {
return( this.day.hashCode() );
}

public String toString() {
return( DateFormat.getDateInstance(DateFormat.MEDIUM).format(day) );
}

...

}

In my program, DailyNotes objects are stored in an ArrayList and accessed randomly by date or sequentially in date order.

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: overriding methods in java.lang.object Posted: Oct 6, 2003 8:09 PM
Reply to this message Reply
I was reading Effective Java by Joshua Bloch and in Item 7 he says its not necessary to Override equals() when one of these condition apply.

1. Each instance of the class is inherently unique
2. Your don't care whether the class provides a "logical equality" test
3. A superclass has already overridden equals, and the behavior inherited from the superclass is appropriate for this class.
4. The class is private or package-private, and you are certain that its equals method will never be invoked.

I guess in other cases you might have overide.

sandesh

Posts: 10
Nickname: sandesh
Registered: Sep, 2003

Re: overriding methods in java.lang.object Posted: Oct 6, 2003 10:51 PM
Reply to this message Reply
hi,
i think the i will have to override my equals as i have to satisfy the 2nd and 3rd points u have mentioned..it was good information...
thanks

Flat View: This topic has 6 replies on 1 page
Topic: Graphics 2D sprayPaint stroke/brush needed Previous Topic   Next Topic Topic: Math Script Ideas

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use