-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOverridenEqualsExample.java
More file actions
72 lines (58 loc) · 3.46 KB
/
OverridenEqualsExample.java
File metadata and controls
72 lines (58 loc) · 3.46 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.util.Objects;
import utility.Printer;
public class OverridenEqualsExample {
private static class Person {
String name;
Person(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{name={'" + name + "'}@"+ Printer.YELLOW_BACKGROUND + name.hashCode()+ Printer.RESET + Printer.GREEN + "}@"+ Printer.RED_BACKGROUND + this.hashCode() + Printer.RESET + Printer.GREEN;
}
}
private static class Student extends Person {
static int count = 0;
int id;
int age;
Student(String name, int age) {
super(name);
this.age = age;
this.id = ++count;
}
@Override
public boolean equals(Object person) {
if (!(person instanceof Student)) return false;
Student student = (Student) person;
return (id == student.id && name.equals(student.name));
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), id, age);
}
@Override
public String toString() {
return "Student{id=" + id + ", name='" + name + "', age=" + age + "}@"+ Printer.RED_BACKGROUND + this.hashCode() + Printer.RESET + Printer.GREEN;
}
}
public static void main(String[] args) {
String firstName = "Shahzad";
String lastName = "Shahzad";
Printer.printColored("Names: ", "Are {'" + firstName + "'}@"+ Printer.YELLOW_BACKGROUND + firstName.hashCode() + Printer.RESET + Printer.GREEN +" and {'" + lastName + "'}@"+ Printer.YELLOW_BACKGROUND + lastName.hashCode() + Printer.RESET + Printer.GREEN +" equal"+ Printer.YELLOW +"? " + Printer.BLUE_BACKGROUND + firstName.equals(lastName));
System.out.println( Printer.YELLOW + "The hashcode of the strings matches, hence, they are equal.\n" + Printer.RESET);
Person person1 = new Person(firstName);
Person person2 = new Person(firstName);
Printer.printColored("Person 1, 2", "Is " + person1 + " equal to " + person2 + Printer.YELLOW + "? " + person1.equals(person2));
System.out.println(Printer.YELLOW + "Despite having the same name, they are not equal because the hashcode of the objects do not match and equals() is not overriden.\n");
Student student1 = new Student(firstName, 20);
Student student2 = new Student(firstName, 20);
Printer.printColored("Student 1, 2", "Is " + student1 + " equal to " + student2 + Printer.YELLOW + "? " + student1.equals(student2));
System.out.println(Printer.YELLOW + "Despite having the same name and age, they are not equal because the IDs do not match according to the equals() method.\n" + Printer.RESET);
Student student3 = new Student(firstName, 20);
student3.id = student1.id; // Manually set the same ID for testing
Printer.printColored("Student 1, 3", "Is " + student1 + " equal to " + student3 + Printer.YELLOW + "? " + Printer.BLUE_BACKGROUND + student1.equals(student3));
System.out.println(Printer.YELLOW + "They are equal according to the overriden equals() method as IDs and names match.\n" + Printer.RESET);
Printer.printColored("Student 2, 3", "Is " + student2 + " equal to " + student3 + Printer.YELLOW + "? " + student2.equals(student3));
System.out.println(Printer.YELLOW + "They are not equal according to the overriden equals() method as IDs do not match.\n" + Printer.RESET);
}
}