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 Point))
	    return false;
	// If o is a normal Point, do a color-blind comparison
	if (!(o instanceof ColorPoint))
	    return o.equals(this);
	// o is a ColorPoint; do a full comparison
	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 transitivity, for example:


ColorPoint p1 = new ColorPoint(1, 2, Color.RED);
Point p2 = new Point(1, 2);
ColorPoint p3 = new ColorPoint(1, 2, Color.BLUE);

Now p1.equals(p2) and p2.equals(p3) return true, while p1.equals(p3) returns false, a clear violation of transitivity. The first two comparisons are “color-blind,” while the third takes color into account.


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 Point))
	    return false;
	// If o is a normal Point, do a color-blind comparison
	if (!(o instanceof ColorPoint))
	    return o.equals(this);
	// o is a ColorPoint; do a full comparison
	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 Point))
	    return false;
	// If o is a normal Point, do a color-blind comparison
	if (!(o instanceof ColorPoint))
	    return o.equals(this);
	// o is a ColorPoint; do a full comparison
	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 transitivity, for example:


ColorPoint p1 = new ColorPoint(1, 2, Color.RED);
Point p2 = new Point(1, 2);
ColorPoint p3 = new ColorPoint(1, 2, Color.BLUE);

Now p1.equals(p2) and p2.equals(p3) return true, while p1.equals(p3) returns false, a clear violation of transitivity. The first two comparisons are “color-blind,” while the third takes color into account.

If you want to change selection, open document below and click on "Move attachment"

pdf

owner: piotr.wasik - (no access) - Effective Java (Joshua Bloch), 2ed, p38

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.