1. class Mine extends Sssh {
2. int z;
int i;
3. public static void main(String[] args) {
4. Mine m1 = new Mine(); m1.i = 5; m1.z = 12;
5. Mine m2 = new Mine(); m2.i = 5; m2.z = 12;
6. if (m1.equals(m2))
7. System.out.println("YES");
8. else
9. System.out.println("NO");
10. }
11.
12. public boolean equals(Object ob) {
System.out.println(z); // Line 1
13. if (z != ((Mine)ob).z)
14. return false; // line 2
15. return super.equals(ob); //line 3
16. }
17. }
Ans: IT IS IMPOSSIBLE TO KNOW.
problem:
1. why is the answer impossible to know.
ans. we don't know about super class equals method.
2. Line1 access which instance of z , ie, m1.z or m2.z . I tried giving a print statement and found that it accessed m1.z(Here the values are same. I gave diff values) . Why is it is accessing m1.z value?
ans. you are calling equals() metod on instance m1,so 'this'
points to m1. Try with m2.equals(m1).
3. Line 2 where is this return going to (I mean it is returning to which line?)
ans. return type is boolean here,so if you give
boolean b=m1.equals(m2);//b will get the return value
4. Line 3 -- can there be 2 return statements.
ans. it can contain any number of return statements,but take
care that all statements are reachable