Java - abstract class, equals () and two subclasses

I have an abstract class called Xpto and two subclasses that extend it with the name Person and Car . I also have a class called Test with main () and a foo () method that checks if two people or cars (or any object of the class that extends Xpto) is equal. So I overridden equals () in the Person and Car classes. Two people are equal if they have the same name, and two cars are equal when they have the same registration.

However, when I call foo () in the Test class, I always get "false". I understand why: equals () is not overridden in the abstract Xpto class. So ... how can I compare two people or cars (or any class object that extends Xpto) in this foo () method?

So this is the code I have:

public  abstract class Xpto {


}

public class Person extends Xpto{

        protected String name;

        public Person(String name){
                this.name = name;
        }

        public boolean equals(Person p){
                System.out.println("Person equals()?");
                return this.name.compareTo(p.name) == 0 ? true : false;
        }
}

public class Car extends Xpto{
        protected String registration;

        public Car(String registration){
                this.registration = registration;
        }

        public boolean equals(Car car){
                System.out.println("Car equals()?");
                return this.registration.compareTo(car.registration) == 0 ? true : false;
        }
}

public class Teste {

        public static void foo(Xpto xpto1, Xpto xpto2){
                if(xpto1.equals(xpto2))
                        System.out.println("xpto1.equals(xpto2) -> true");
                else
                        System.out.println("xpto1.equals(xpto2) -> false");

        }

        public static void main(String argv[]){
                Car c1 = new Car("ABC");
                Car c2 = new Car("DEF");
                Person p1 = new Person("Manel");
                Person p2 = new Person("Manel");

                foo(p1,p2);
        }
}
+3
source share
10 answers

As others say, the signature of the method you redefine must be exactly the same. When overriding methods, to make sure that you override, use the note @Overrideabove the function, so IDEs such as Eclipse will warn you if you change the method.

Here's what it looks like:

@Override
public boolean equals(Object obj){
...Your code here...
}

hashCode(), , , hastables .. ( performande) hashCode() ( equals() !)

, :

@Override
public boolean equals(Object obj){
...Your code here...
}

@Override
public int hashCode(){
...Your code here...
}

javadoc

+4

, : equals() Xpto.

equals() . , Object , ( instanceof false, ).

+2

public boolean equals(Object o) ?

+1

Javadoc , equals .

, - "" .

, :

public class Car extends Xpto
{
    protected String registration;

    public Car(String registration)
    {
        this.registration = registration;
    }

    public boolean equals(Object obj)
    {
        if (obj == null)
        {
            return false;
        }
        if (obj == this)
        {
            return true;
        }
        if (!obj.getClass().isAssignableFrom(getClass()))
        {
            return false;
        }
        Car car = (Car) obj;
        return this.registration.compareTo(car.registration) == 0 ? true : false;
    }
}
+1

public boolean equals (Person p) public boolean equals (Car p) Object public boolean equals (Object o), , .

+1

equals :

@Override public boolean equals(Object o) {
   if (!(o instanceof YourType)) {
      return false;
   }
   YourType yt = (YourType)o;
   ... // rest here
}

, hashCode, .

+1

, / - , , , , . equals ( , ).

equals Xpto, . - Xpto, Xpto:

 public class Xpto {
        protected abstract String getIdentity();

        @Override
        public boolean equals(Object o) {
            if (o == null) return false;
            //Typical implementation
            //if (getClass() != o.getClass()) return false;
            if (!(o instanceof Xpto)) return false; //risky implementation, but will allow a car to compare to a person
             return getIdentity().equals((Xpto) o.getIdentity());
        }

        @Override
        public int hashCode() {
             return getIdentity().hashCode();
        }
  }

, . , , @Override. , .

+1

:

public  abstract class Xpto {

}

public class Person extends Xpto{

    protected String name;

    public Person(String name){
            this.name = name;
    }

    public boolean equals(Object o){
       if(o == null || !getClass().equals(o.getClass())
          return false;
       Person p = (Person) o;
       System.out.println("Person equals()?");
       return this.name.compareTo(p.name) == 0 ? true : false;
    }
}

public class Car extends Xpto {
    protected String registration;

    public Car(String registration){
            this.registration = registration;
    }

    public boolean equals(Object o){
       if(o == null || !getClass().equals(o.getClass())
          return false;
       Car car = (Car) o;
       System.out.println("Car equals()?");
       return this.registration.compareTo(car.registration) == 0 ? true : false;
    }
}

public class Teste {

    public static void foo(Xpto xpto1, Xpto xpto2){
            if(xpto1.equals(xpto2))
                    System.out.println("xpto1.equals(xpto2) -> true");
            else
                    System.out.println("xpto1.equals(xpto2) -> false");

    }

    public static void main(String argv[]){
            Car c1 = new Car("ABC");
            Car c2 = new Car("DEF");
            Person p1 = new Person("Manel");
            Person p2 = new Person("Manel");

            foo(p1,p2);
    }
}

equals(Object) Object. , Xpto .

( : Person, Car) . , equals Object, .

0

equals(), .

public boolean equals(Object o)

o Person/Car .

BTW, equals():

return registration.equals(car.registration);
0

(Person) equals (Car), Xpto. (Xpto), , (Object), , .

, equals() , (1) , , , ( 2) , , getHashCode(), (Object), getHashCode() - .

0

Source: https://habr.com/ru/post/1742452/


All Articles