File tree Expand file tree Collapse file tree 2 files changed +44
-0
lines changed
Java/OverridingEqualsMethod Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments