Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 10th, 2006, 01:39 AM
Registered User
 
Join Date: Mar 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to override equals() and hashcode()

can anyone plz guide me how to override these two=equals() and hashcode () methods?
i never understand how to implement them.can someone plz give an example to show how to override these two..
plz plz i need help on this topic..
thanks

 
Old March 14th, 2006, 02:08 AM
arp arp is offline
Authorized User
 
Join Date: Jun 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

You can not override equals method from java.lang.String class since String class is FINAL class.
But you can override equals() method in any other class since all classes are implicitly inherited from Object class which is NOT FINAL.
Following is sample code stored in "testOverriding.java" file. Here equals method is used to display string instead of comparison.

class newequals
{
   public void equals(String str){
     System.out.println(str);
    }
}

public class testOverriding
{
    public static void main(String[] args){
    newequals test = new newequals();
    test.equals("Test String");
    }
}

----------------
OUTPUT
---------------

Test String


 
Old March 14th, 2006, 02:20 AM
Wrox Technical Editor
 
Join Date: Dec 2005
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

1. public class Test
2. {
3. private int num;
4. private String data;
5.
6. public boolean equals(Object obj)
7. {
8. if(this == obj)
9. return true;
10. if((obj == null) || (obj.getClass() != this.getClass()))
11. return false;
12. // object must be Test at this point
13. Test test = (Test)obj;
14. return num == test.num &&
15. (data == test.data || (data != null && data.equals(test.data)));
16. }
17.
18. public int hashCode()
19. {
20. int hash = 7;
21. hash = 31 * hash + num;
22. hash = 31 * hash + (null == data ? 0 : data.hashCode());
23. return hash;
24. }
25.
26. // other methods
27. }

Here's a link to a tutorial (where the preceeding code was taken from): http://www.geocities.com/technofundo...equalhash.html
See the link above for the complete tutorial.

- A.Kahtava
 
Old March 14th, 2006, 02:30 AM
arp arp is offline
Authorized User
 
Join Date: Jun 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry !!
for my previous post that was example of Overloading I have modified the code below which represents Overriding for equals() and hashcode()

You can not override equals method from java.lang.String class since String class is FINAL class.
But you can override equals() method in any other class since all classes are implicitly inherited from Object class which is NOT FINAL.
Following is sample code stored in "testOverriding.java" file. Here equals method is used to display string instead of comparison.

class BaseMethods
{
   BaseMethods(){
   System.out.println("Constructor called for Base Methods\n");
   }
}

class OveriddenMethods
{
   public void equals(){
    System.out.println("Equals Overridden\n");
   }

   public int hashCode() {
    System.out.println("Hashcode Overridden\n");
    return 100;
   }
}

public class testOverriding
{
  public static void main(String[] args){
   int returnVal =0;
   String s1 = "abcd";
   String s2 = "abcd";

  System.out.println("Base equals method result = " +s1.equals(s2) + "\n");

  BaseMethods base = new BaseMethods();
  returnVal =base.hashCode();
  System.out.println("returnVal = " + returnVal + "\n");

 // equals() and hashCode methods are Overridden below in OveriddenMethods Class

 OveriddenMethods test=new OveriddenMethods();
   test.equals();
   returnVal = test.hashCode();
   System.out.println("returnVal = " + returnVal + "\n");
  }
}

----------------
OUTPUT
---------------
Base equals method result = true

Constructor called for Base Methods

returnVal = 15655788

Equals Overridden

Hashcode Overridden

returnVal = 100
----------------------------------

Thanks,

ARP






Similar Threads
Thread Thread Starter Forum Replies Last Post
Refresh the browser equals insert a new record!? Magi BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 30 November 18th, 2007 06:01 PM
problem with equals() and == praveena Java Basics 7 June 15th, 2007 08:05 AM
SQL "equals operator" in queries Steve777 SQL Language 1 June 24th, 2005 10:42 AM
To Override or Not to Override frresh BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 1 May 2nd, 2005 01:33 PM
Rules on use of == double equals (see p142, 146) ababb BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 January 20th, 2005 05:58 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.