KaiquanMah commited on
Commit
cc4c404
·
verified ·
1 Parent(s): 8b5ef9d

this == obj; OR Class1 var1 = (Class1) obj1; (x == obj1.x && y == obj1.y);

Browse files
Week 5: Class hierarchies/13A. equals Method ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ We have previously used the equals method to compare String objects.
2
+ It is also possible to implement the method in own class.
3
+ This enables the COMPARISON of 2 OBJECTS created out of OWN CLASS.
4
+
5
+ Let's implement the method to class 'Point', which models a point in 2-dimensional coordinate system.
6
+
7
+ Toteutetaan siis metodi equals luokkaan Piste, joka mallintaa pistettä kaksiulotteisessa koordinaatistossa:
8
+
9
+ class Point {
10
+ private int x;
11
+ private int y;
12
+
13
+ //CONSTRUCTOR
14
+ public Point(int x, int y) {
15
+ this.x = x;
16
+ this.y = y;
17
+ }
18
+
19
+
20
+ @Override
21
+ public boolean equals(Object obj) {
22
+ // If same object, must be equal
23
+ if (this == obj) {
24
+ return true;
25
+ }
26
+
27
+ // Jos other is null, must be false
28
+ if (obj == null) {
29
+ return false;
30
+ }
31
+
32
+ // If other is not Point, must be false
33
+ if (obj.getClass() != Point.class) {
34
+ return false;
35
+ }
36
+
37
+
38
+ // OTHERWISE IF
39
+ // 1 not the same object OR
40
+ // 2 not null -> good OR
41
+ // 3 not a Point class
42
+ // -> Cast the input 'obj' as a Point object
43
+ Point other = (Point) obj;
44
+
45
+ //THEN CHECK
46
+ // If x and y are equal, points are equal
47
+ return (x == other.x && y == other.y);
48
+
49
+ }
50
+ }
51
+
52
+
53
+
54
+
55
+ The implementation of the method (usually) follows the same structure.
56
+ Note that it's up to the programmer to decide when two objects should be equal: do ALL ATTRIBUTES need to be EQUAL OR ONLY SOME?
57
+
58
+ For example, we can decide that 2 Student objects are equal if they have
59
+ 1 - the 'same student id' - as the rest of the attributes are not important
60
+ 2 - (but then again, in some other case we may require that 'all attributes' have equal value for 'students' to be equal):
61
+
62
+
63
+ class Student extends Person {
64
+ // Name and email are inherited from Person
65
+ private String studentId;
66
+ private int credits;
67
+
68
+ public Student(String studentId, String name, String email, int credits) {
69
+ super(name, email);
70
+ this.studentId = studentId;
71
+ this.credits = credits;
72
+ }
73
+
74
+ @Override
75
+ public boolean equals(Object obj) {
76
+ // SAME OBJECCT
77
+ if (this == obj) {
78
+ return true;
79
+ }
80
+
81
+ // NOT NULL
82
+ if (obj == null) {
83
+ return false;
84
+ }
85
+
86
+ // not 'Student' class
87
+ if (obj.getClass() != Student.class) {
88
+ return false;
89
+ }
90
+
91
+ // SAME studentId
92
+ Student other = (Student) obj;
93
+ // Strings are compared with equals method
94
+ return studentId.equals(other.studentId);
95
+ }
96
+ }
97
+
98
+
99
+
100
+
101
+ A typical error is to forget to use the 'equals' method when comparing objects:
102
+ for example, String objects' equality must be checked with equals method instead of == operator.
103
+
104
+ Most editors provide a way to insert the equals method automatically,
105
+ plese refer to your editor's documentation (or google it - for example "visual studio code java equals".
106
+
107
+
108
+
109
+
110
+
111
+