0

So i've created equals() method. If i want to compare 2 objects, they can only see this one. How do i get my object to use the one defined in the object class.

public boolean equals(Object object1) {
if(a == object1) {   
    return true;
}
else return false;
}

public static void main(String[] args) {
Compare object1 = new Compare("test");
Compare object2 = new Compare("test");
if(object1.equals(object2)
 .................

}
Square-root
  • 853
  • 1
  • 12
  • 25

1 Answers1

0

How do i get my object to use the one defined in the object class.

If you don't override equals in your class, it will use by default the one declared in closet parent class. If no closest parent class has it then Object class has one which will be used.

But if you want to call equals from Objectclass in any of the method of your class you need to use super which is a reference to parent. You need to use super.equals().

akhil_mittal
  • 23,309
  • 7
  • 96
  • 95
  • It will use the one in the closest parent class overriding `equals`, more precisely – Dici Oct 01 '15 at 00:42
  • @Dici: Yes you are right. – akhil_mittal Oct 01 '15 at 00:45
  • Yes, i'd like to use the one in object class. is this correct? if(object1.super.equals(object2)) – Square-root Oct 01 '15 at 00:47
  • @Square-root The implementation in `Object` simply uses `==`. – Sotirios Delimanolis Oct 01 '15 at 00:49
  • 1
    @Square-root: If your class or any of its parent class (except Object) has nto overridden `equals` method then by default one from `Object` will be used. The `equals` in Object checks for reference equality which you may not be really needing. – akhil_mittal Oct 01 '15 at 00:50
  • isn't that compares references rather than values which i'm looking to compare – Square-root Oct 01 '15 at 00:50
  • @Square-root: You are trying to compare two references which point to two different instances (notice `new`). – akhil_mittal Oct 01 '15 at 00:52
  • i'm comparing the values that's why i'm using .equal() .. i understand all of your points but i want to call the one in object while keeping my own version of .equal(). is this syntactically correct if(object1.super.equals(object2)) because it tell me the object cannot find the symbol super – Square-root Oct 01 '15 at 00:56
  • This is not clear... if you want to use the default implementation just use `==` but this is doomed to fail because you know the references are different. What's wrong in your code is your implementation of `equals` which never considers the attributes of your class – Dici Oct 01 '15 at 00:58
  • the == will just show me the 2 objects referring to different places in memory.. rather, i'd like to compare the values so == will not help me with that – Square-root Oct 01 '15 at 01:01
  • 1
    @Square-root Then there's no point in using `Object`'s implementation of `equals`. – Sotirios Delimanolis Oct 01 '15 at 01:10
  • 1
    Yeah, that's what everyone has been trying to tell you. `Object.equals` uses `==`, so you don't want to use it. You have to override `equals` to take into account the value of the attribute, that's simple as this – Dici Oct 01 '15 at 01:10