Skip to content

Commit 475026a

Browse files
committed
codeX1616-Code to override Object.equals() method in any java class
1 parent ac812fc commit 475026a

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

Java/OverridingEqualsMethod/Abc.class

675 Bytes
Binary file not shown.

Java/OverridingEqualsMethod/Abc.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
public class Abc {
2+
3+
public String getString() {
4+
return string;
5+
}
6+
7+
public int getValue() {
8+
return value;
9+
}
10+
11+
private String string;
12+
private int value;
13+
14+
public Abc(String string, int value) {
15+
this.string = string;
16+
this.value = value;
17+
}
18+
19+
@Override
20+
public boolean equals(Object object) {
21+
if (object == this) {
22+
return true;
23+
}
24+
if (!(object instanceof Abc)) {
25+
return false;
26+
}
27+
Abc abc = (Abc) object;
28+
return string.equals(abc.getString()) && value == abc.getValue();
29+
}
30+
31+
}
666 Bytes
Binary file not shown.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class EqualsCheck {
2+
public static void main(String[] args) {
3+
Abc abc1 = new Abc("abc1", 1);
4+
Abc abc2 = new Abc("abc1", 1);
5+
6+
boolean check = abc1.equals(abc2);
7+
if (check) {
8+
System.out.println("Both objects are equal");
9+
} else {
10+
System.out.println("Both objects are unequal");
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)