Results 1 to 4 of 4
  1. #1
    ahanslot is offline Member
    Join Date
    Sep 2013
    Posts
    26
    Rep Power
    0

    Default overriding equals method

    I am trying to override the equals method for my class TeamMember I have two fields:

    private Details details;
    private double salary;

    Code for override I get an error on salary saying cannot invoke equals (double) on the primitive type (double)

    Is their something I am missing/coding wrong?

    Java Code:
    public boolean equals(Object obj) {
    		//test exceptional cases
    		//avoid potential NullPointerException/ClassCastException
    		if ((obj == null) || (this.getClass() != obj.getClass()))
    			return false;
    		TeamMember other = (TeamMember) obj; //cast to a TeamMember object
    		// compare fields details and salary
    		return this.details.equals(other.details)
    				&& this.salary.equals(other.salary);
    	}

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: overriding equals method

    If it is a primitive double simply use ==

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: overriding equals method

    The salary field is a double; doubles are prmitives and don't have an equals( ... ) method (they don't have any method at all); make it: this.salary == other.salary.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    ahanslot is offline Member
    Join Date
    Sep 2013
    Posts
    26
    Rep Power
    0

    Default Re: overriding equals method

    Quote Originally Posted by JosAH View Post
    The salary field is a double; doubles are prmitives and don't have an equals( ... ) method (they don't have any method at all); make it: this.salary == other.salary.

    kind regards,

    Jos

    thanks :)

Similar Threads

  1. Method Overriding
    By vini11 in forum New To Java
    Replies: 6
    Last Post: 02-13-2012, 07:56 PM
  2. What is use of method overloading and overriding
    By srinivasmallabathula in forum New To Java
    Replies: 3
    Last Post: 04-06-2011, 08:07 AM
  3. Overriding equals method
    By sky in forum New To Java
    Replies: 7
    Last Post: 03-12-2010, 04:39 PM
  4. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 08:20 PM
  5. Method Overriding - Seriously confused :-(
    By fullmetaljacket in forum New To Java
    Replies: 4
    Last Post: 05-26-2009, 05:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •