-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectMethods.java
More file actions
35 lines (32 loc) · 1.26 KB
/
ObjectMethods.java
File metadata and controls
35 lines (32 loc) · 1.26 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
package JavaLangPackage;
// This class inherited from Object Class
class MyObject extends Object{
@Override
public String toString(){
return "Overwritten toString";
}
@Override
public int hashCode(){
return 999;
}
public boolean equals(MyObject O){
return this.hashCode()==O.hashCode();
// return true if the object which called this equals() have hashCode as O hashCode.
}
}
public class ObjectMethods {
public static void main(String[] args) {
MyObject O1 = new MyObject(); // first Object created and O1 holding that object
System.out.println(O1); // Will call toString() by default
System.out.println(O1.toString());
System.out.println(O1.hashCode()); // Unique ID generated by JVM using Address
MyObject O2 = new MyObject(); // second Object created O2 holding that object
System.out.println(O2);
System.out.println(O2.hashCode());
System.out.println(O1.equals(O2)); // return whether they are pointing same object or not
O1 = O2; // O1 reference updated with O2 reference
System.out.println(O1.equals(O2));
System.out.println(O1.hashCode()); // Both will have same hashcode
System.out.println(O2.hashCode());
}
}