Do you want BuboFlash to help you learning these things? Or do you want to add or correct something? Click here to log in or create user.



Tags
#bloch-effective-java-2ed #java
Question

Suppose we have a class


public class Point {
    private final int x;
    private final int y;

    @Override public boolean equals(Object o) {
	if (!(o instanceof Point))
	    return false;
	Point p = (Point)o;
	return p.x == x && p.y == y;
    }
    // Remainder omitted
}

and a subclass

public class ColorPoint extends Point {
    private final Color color;

    @Override public boolean equals(Object o) {
      if (!(o instanceof ColorPoint))
         return false;
      return super.equals(o) && ((ColorPoint) o).color == color;
     }

    // Remainder omitted
}

what are the consequences of implementing equals() method on ColorPoint like above?

Answer
It violates symmetry. You get different results when comparing a point to a color point and vice versa.

Tags
#bloch-effective-java-2ed #java
Question

Suppose we have a class


public class Point {
    private final int x;
    private final int y;

    @Override public boolean equals(Object o) {
	if (!(o instanceof Point))
	    return false;
	Point p = (Point)o;
	return p.x == x && p.y == y;
    }
    // Remainder omitted
}

and a subclass

public class ColorPoint extends Point {
    private final Color color;

    @Override public boolean equals(Object o) {
      if (!(o instanceof ColorPoint))
         return false;
      return super.equals(o) && ((ColorPoint) o).color == color;
     }

    // Remainder omitted
}

what are the consequences of implementing equals() method on ColorPoint like above?

Answer
?

Tags
#bloch-effective-java-2ed #java
Question

Suppose we have a class


public class Point {
    private final int x;
    private final int y;

    @Override public boolean equals(Object o) {
	if (!(o instanceof Point))
	    return false;
	Point p = (Point)o;
	return p.x == x && p.y == y;
    }
    // Remainder omitted
}

and a subclass

public class ColorPoint extends Point {
    private final Color color;

    @Override public boolean equals(Object o) {
      if (!(o instanceof ColorPoint))
         return false;
      return super.equals(o) && ((ColorPoint) o).color == color;
     }

    // Remainder omitted
}

what are the consequences of implementing equals() method on ColorPoint like above?

Answer
It violates symmetry. You get different results when comparing a point to a color point and vice versa.
If you want to change selection, open document below and click on "Move attachment"

pdfs

  • owner: piotr.wasik - (no access) - Effective Java (Joshua Bloch), 2ed, p37
  • owner: Jack-Rustier - (no access) - Joshua Bloch-Effective Java-Addison-Wesley (2017).pdf, p62

Summary

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

Details

No repetitions


Discussion

Do you want to join discussion? Click here to log in or create user.