Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2024
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. class A {
  2. private int a;
  3. public A(int a) { this.a = a; }
  4.  
  5. @Override
  6. public final boolean equals(Object o) {
  7. // Check if the exact classes are the same
  8. if (this.getClass() != o.getClass()) return false;
  9. return this.a == ((A) o).a;
  10. }
  11. }
  12.  
  13. class B extends A {
  14. private int b;
  15.  
  16. public B(int a, int b) { super(a); this.b = b; }
  17. }
  18.  
  19. A a = new A(0);
  20. B b = new B(0, 1);
  21.  
  22. System.out.println(a.equals(b)); // false
  23. System.out.println(b.equals(a)); // false
  24.  
  25. System.out.println(a.equals(a)); // true
  26. System.out.println(b.equals(b)); // true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement