Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2024
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. class A {
  2. private int a;
  3. public A(int a) { this.a = a; }
  4.  
  5. @Override
  6. public 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. @Override
  19. public boolean equals(Object o) {
  20. if (!super.equals(o)) return false;
  21. if (this.getClass() != o.getClass()) return false;
  22. return this.b == ((B) o).b;
  23. }
  24. }
  25.  
  26. A a = new A(0);
  27. B b = new B(0, 1);
  28.  
  29. System.out.println(a.equals(b)); // false
  30. System.out.println(b.equals(a)); // false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement