fork download
  1. public class Main {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. C c = new C("Test", 10);
  6. D d = new D("Test", 10);
  7.  
  8. if (c.equals(d))
  9. System.out.println("Equal");
  10. else
  11. System.out.println("Unequal");
  12.  
  13. if (d.equals(c))
  14. System.out.println("Equal");
  15. else
  16. System.out.println("Unequal");
  17. }
  18. }
  19.  
  20.  
  21. class C
  22. {
  23. C(String cstr, int cnum) {
  24. str = cstr;
  25. num = cnum;
  26. }
  27.  
  28. @Override
  29. public boolean equals(Object otherObject) {
  30.  
  31. // A quick test to see if the objects are identical.
  32. if (this == otherObject) {
  33. return true;
  34. }
  35.  
  36. // Must return false if the explicit parameter is null
  37. if (otherObject == null)
  38. {
  39. return false;
  40. }
  41.  
  42. if (!(otherObject instanceof C))
  43. return false;
  44.  
  45. // Now we know otherObject is a non-null Employee
  46. C other = (C) otherObject;
  47.  
  48. // Test whether the fields have identical values
  49. return str.equals(other.str) && num == other.num;
  50. }
  51.  
  52. private String str;
  53. private int num;
  54. }
  55.  
  56. class D extends C {
  57.  
  58. D(String cstr, int cnum) {
  59. super(cstr, cnum);
  60. }
  61. }
Success #stdin #stdout 0.1s 320320KB
stdin
Standard input is empty
stdout
Equal
Equal