Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Fragile {
- public static void main(String... args) {
- final var a = new A(0);
- final var b = new B(0);
- System.out.println("A equals B? " + a.equals(b));
- System.out.println("B equals A? " + b.equals(a));
- }
- }
- class A {
- protected final int n;
- public A(int p) { n = p; }
- public final int hashCode() { return n; }
- public final boolean equals(Object other) {
- if (other.getClass() == this.getClass()) {
- return n == ((A) other).n;
- }
- return false;
- }
- }
- class B extends A {
- public B(int p) { super(p); }
- public int hashCode() { return n; }
- public final boolean equals(Object other) {
- return other instanceof B && n == ((B) other).n;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement