Results 1 to 8 of 8
  1. #1
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default [SOLVED]Overriding equals method

    Hi,

    Acording to the Java API, the method indexOf of the class Vector is built on top of the method equals. When I try to override the method in my class, the result is not the one I expected. I made a small example to ilustrate the problem:

    Java Code:
    public class Movie {
       
       private String mtitle;
       private int bandwidth;
       
       public Movie (String mtitle, int bandwidth) {
          this.mtitle = mtitle;
          this.bandwidth = bandwidth;
       }
       
       public int getBandwidth() {
          return bandwidth;
       }
       
       public String getTitle() {
          return mtitle;
       }
       
       @Override
       public boolean equals(Object mtitle) {
          return (this.mtitle).equals((String)mtitle);
       }
       
    }
    Java Code:
    import java.util.Vector;
    
    public class Example {
    
       Vector<Movie> movies = new Vector<Movie>();
       
       private Example() {
       
          int index;
          Movie movie = new Movie("mtitle",50);
          movies.add(movie);
          
          if ((index = movies.indexOf("mtitle")) == -1) {
             System.out.println("The movie is not in the database");
          } else {
             Movie movie1 = movies.get(index);
             System.out.println("The bandwidth is "+movie1.getBandwidth());
          }
       }
    
       public static void main(String[] args) {
          Example ex = new Example();
       }
    }
    When I run the previous program, the answer of the program should be the bandwidth, but I get that the movie is not in the database instead. I know that I am passing a String as an argument to the method indexOf, and the Vector is made of Movies, but if the method equals is used, then it should work. Therefore I guess that my question is how the method indexOf actually works. Any hints are appreciated.
    Last edited by sky; 03-12-2010 at 04:40 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Your equals method appears to be comparing the current object with a String, and that goes against equals contract. It should only compare the current object with another object of the same type.

    I recommend that you:
    1) first check that the parameter is not null. If it is null, return false.
    2) next check that the parameter is an instanceof the current class. If not return false.
    3) next cast the parameter as an object of the current class, and THEN check that the parameter's mtitle string matches the current object's mtitle String.

  3. #3
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    The thing is I do not want to compare the objects, I am only interested in comparing the field mtitle. So, as far as I understood, I cannot write it like this:

    Java Code:
    index = movies.indexOf("mtitle")
    But I have to use a a Movie object instead;

    Java Code:
    Movie movieAux("mtitle",0);
    index = movies.indexOf(movieAux)
    Is that correct?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Next: indexOf will need to be passed an object of Movie type to work, not a String.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by sky View Post
    The thing is I do not want to compare the objects, I am only interested in comparing the field mtitle. So, as far as I understood, I cannot write it like this:
    If you are using equals, you have no choice in this matter, you MUST compare Movie objects. Period.

    Java Code:
    index = movies.indexOf("mtitle")
    But I have to use a a Movie object instead;

    Java Code:
    Movie movieAux("mtitle",0);
    index = movies.indexOf(movieAux)
    Is that correct?
    Yes.

  6. #6
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    Ok, thank you very much for the quick answer. Now I am wondering if there is another way to do it, without having to create a new useless Movie object.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Sure, iterate through the Vector in a for loop and do your own comparison of a title String with the current Vector item's title string using String's equals method.

  8. #8
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    There is no shortcut then, I will have to iterate by myself. Thanks again.

Similar Threads

  1. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 08:20 PM
  2. Method Overriding - Seriously confused :-(
    By fullmetaljacket in forum New To Java
    Replies: 4
    Last Post: 05-26-2009, 05:18 PM
  3. [SOLVED] Method Overriding
    By MithunDhar in forum New To Java
    Replies: 3
    Last Post: 04-06-2009, 09:02 AM
  4. Replies: 1
    Last Post: 04-01-2009, 03:00 AM
  5. is overriding static method possible
    By raghu in forum Advanced Java
    Replies: 1
    Last Post: 01-22-2008, 01:38 AM

Posting Permissions

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