This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes override method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "override method" Watch "override method" New topic
Author

override method

Ben Buchli
Ranch Hand

Joined: Mar 26, 2004
Posts: 83
Hi there,
I have an assignment to implement a ListArray ADT and ListLinked ADT. I basically have to implement the equivalent to the java ArrayList and LinkedList classes. This is not a problem and I'm pretty much done, the only problem however is, that a requirement is to override the "equals" method of any class of objects that must be stored in the list. Now how do I do that? Any help is greatly appreciated!
Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
Hi Ben,

You ask how to override the equals method.

Your job is to implement an equals method,
public boolean equals(Object theOtherObject)
which will return true if the two objects are equal and false if not.

About how to do it - do you know what types of objects you'll
have in your list?

For example: Objects called "Numbers" which has a instance member
called "value" - in that case you might decide to say that two
numbers a and b are equals if they have the same value.

If you don't know what kinds of objects you'll get you can only
return true if the objects are the same.

Now - I intentionally didn't supply some code as I didn't wan't to
ruin your fun

If you still have problems with the equals method, then reply and I'll
give another hint or a few lines of code.

/Svend Rost
[ September 23, 2004: Message edited by: Svend Rost ]
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
I'm assuming you mean the equals(java.lang.Object) method of class java.lang.Object. Before moving on, it is <b>imperative</b> that when you override this method, you meet it's general contract. In order to do that you have to also override the hashCode() method of java.lang.Object.

So a more correct requirement is "meet the general contracts of equals/hashCode and also overriding them to provide deep equality for my types to be stored in the lists."

Here is a sample chapter from a book that gives a 'formula' for achieving that: http://developer.java.sun.com/developer/Books/effectivejava/Chapter3.pdf

Here is some software that will help you make that assertion (if you use JUnit): http://xdweb.net/~dibblego/junitx


Tony Morris
Java Q&A (FAQ, Trivia)
Ben Buchli
Ranch Hand

Joined: Mar 26, 2004
Posts: 83
thanks for your help guys, especially for not posting code. I need to understand this...

Anyway, I have to implement a ListInterface provided by my teacher. In the method requirement of contains() it says:

This method compares the incoming data item with the
items in the list using the "equals" method of the object.
Be sure to override the "equals" method of any class of objects
that must be stored in such a list.

I know how to override methods, but I dont know where I would do it. In my ListArray class??? Also, if I understand you correctly, Svend, if I dont know what type of objects will be stored in this list, the only test for equality would be object1.equals( object2 ) which would return true only if they reference the same object, correct? So in this case I would not have to override the equals method. Correct??
I hope my questions make sense.
thanks again for helping
Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
I know how to override methods, but I dont know where I would do it. In my ListArray class???


The equals method tests weather or not the current object is equal
with the object you pass as an argument. That means, that if you wan't
your List to contain, say Persons then you might make a class called
Person, and in Person you'll override the equals method.

Also, if I understand you correctly, Svend, if I dont know what type of objects will be stored in this list, the only test for equality would be object1.equals( object2 ) which would return true only if they reference the same object, correct?


Yes. But, are you sure you don't know what types of objects you'll have in
your list ? It would not make sence to override the equals method
unless you want to store some specific types of objects.


/Svend Rost
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: override method