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

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
}

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
?

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
}
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, p40

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.