forked from AndrewProgramming/JavaTutorialCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEqualsMethodDemo.java
More file actions
37 lines (28 loc) · 800 Bytes
/
EqualsMethodDemo.java
File metadata and controls
37 lines (28 loc) · 800 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package objectClass;
public class EqualsMethodDemo {
public static void main(String[] args) {
Car audi = new Car("audi", "red");
Car GLE = new Car("benz", "yellow");
Car smart = new Car("benz", "yellow");
// //This is true
// System.out.println(audi.equals(audi));
// //This is not true
// System.out.println(audi.equals(GLE));
//This is true
System.out.println(GLE.equals(smart));
}
}
class Car {
private String name;
private String color;
public Car(String name, String color) {
this.name = name;
this.color = color;
}
public boolean equals(Object o) {
if (o instanceof Car)
return name == ((Car) o).name;
else
return this == o;
}
}