0

In this we are overriding the equals() then it should compare the contents , but it is comparing the reference over here. Why is it so happening?

import java.util.Vector;    
public class Lab1281 {
    public static void main(String[] args) {
        Vector v = new Vector();
        Student12 stu = new Student12(56);
        v.addElement(stu);
        System.out.println(v.contains(new Student12(56)));
    }
}
class Student12 {
    int sid;
    public Student12(int sid) {
        super();
        this.sid = sid;
    }
    public boolean equals(Object obj) {
        System.out.println("**equals()**");
        return super.equals(obj);
    }
}
Pragya
  • 21
  • 2

4 Answers4

0

Looking up Source code gives us , that vector does go to the super class of AbstractList for equals.

public boolean More ...equals(Object o) {
  if (o == this)
        return true;
    if (!(o instanceof List))
        return false;
    ListIterator<E> e1 = listIterator();
    ListIterator e2 = ((List) o).listIterator();
    while(e1.hasNext() && e2.hasNext()) {
       E o1 = e1.next();
        Object o2 = e2.next();
        if (!(o1==null ? o2==null : o1.equals(o2)))
           return false;
    }
    return !(e1.hasNext() || e2.hasNext());
}

As you can the first thing it does is compare references.

user3224416
  • 522
  • 5
  • 15
  • But only to verify that they're the same. It'll fall through to the `equals()` implementation when it sees that they are not. – azurefrog Aug 19 '14 at 14:45
0

Your equals() method is simply returning super.equals(obj);, i.e. the equals() implementation on Object:

public boolean equals(Object obj) {
    System.out.println("**equals()**");
    return super.equals(obj);             // calls Object#equals()
}

If we look at the implementation of Object#equals(), we see that it is doing a straight object reference comparison:

/**
 * Indicates whether some other object is "equal to" this one.
 * 
 * ...
 * 
 * @param   obj   the reference object with which to compare.
 * @return  {@code true} if this object is the same as the obj
 *          argument; {@code false} otherwise.
 * @see     #hashCode()
 * @see     java.util.HashMap
 */
public boolean equals(Object obj) {
    return (this == obj);
}

Since you've created a new Student12 object in your test statement, it returns false.

You need to make your equals() method check for some relevant state in your class:

    @Override
    public boolean equals(Object obj) {
        System.out.println("**equals()**");
        if (!(obj instanceof Student12)) { return false; }
        return ((Student12)obj).sid == this.sid;
    }
azurefrog
  • 10,785
  • 7
  • 42
  • 56
0

Because you are calling super.equals. I will use object equals. Its a reference. Instead calling super you have to write your own equals method.

Siva Kumar
  • 1,983
  • 3
  • 14
  • 26
0

supper class equal() method only used == operator by default for compare the object and you are overriding super class equal() method so it will compare only on reference of object not the actual object content. To compare the actual object content,you should have your own implementation(by using member of your class).

class abc{
int a;
string b
...
...
...
public boolean equal(Object obj){
...
...
this.a==obj.a; //or you can use equal() method here to compare the particular member
this.b==obj.b;

}

}

or you can see here Why do I need to override the equals and hashCode methods in Java?

Community
  • 1
  • 1
sky
  • 93
  • 2
  • 3
  • 15