Edited, memorised or added to reading queue

on 18-Jul-2014 (Fri)

Do you want BuboFlash to help you learning these things? Click here to log in or create user.

Flashcard 149626013

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.


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

pdf

cannot see any pdfs







Flashcard 149626024

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

What are the consequences of implementing equals() method like this (using getClass() instead of using instanceof)?


public class Point {
     @Override public boolean equals(Object o) {
     if (o == null || o.getClass() != getClass())
         return false;
     Point p = (Point) o;
     return p.x == x && p.y == y;
 }
}
Answer
Liskov substitution principle is violated. For example, we cannot mix Points and objects of any subclass of Point in collections, because Points and objects of any subclass of Point will never be equal, even if subclassing is NOT adding any value field.

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

pdf

cannot see any pdfs







Flashcard 149626035

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

What is a better way of implementing ColorPoint to avoid problems with equals() method?

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
}
public class ColorPoint extends Point {
    private final Color color;

    // Remainder omitted
}
Answer

Use composition, not inheritance:

public class ColorPoint {
    private final Point point;
    private final Color color;

    // Remainder omitted
}

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

pdf

cannot see any pdfs







Flashcard 149626046

Tags
#bloch-effective-java-2ed #equality #java
Question
Under what circumstances you can add equals() method to a subclass without causing problems?
Answer
When the superclass is not instantiable (abstract)

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

pdf

cannot see any pdfs