0

Two questions

/*first: It runs while We create the the int x private and actually must not be accessed out of the its own class and with instantiation. And second : When we cast object o to A with code :( (A)o).x and compare to the this.x actually Reference types are compared and they are not equal so why they are equal and return true? class A { private int x; public boolean equals(Object o) { return ((A)o).x == this.x; } public static void main(String args[ ]) { A a = new A(); a.x = 9; A b = new A (); b.x = 5; System.out.println(a.equals (b)); } }

4th Sep 2020, 7:31 AM
Amir Hoseini
Amir Hoseini - avatar
2 ответов
+ 1
You are trying to assign x, but it is private?
4th Sep 2020, 7:50 AM
Roy Mullis
Roy Mullis - avatar
0
It's not outside class, it's within the class so you can access x.. Object is parent class for any class, you can cast to any child type class.. And It returns false, not true, There ((A)o).x is 5, you passed b object so it's b.x and this.x = a.x = 9. So 5 == 9 => return false
4th Sep 2020, 3:35 PM
Jayakrishna 🇮🇳