You are given the class file for a class called Secret. However, you do not have the source code or any information about the internals of the Secret class. You do know that the class has a protected int variable called i. What does the following application print out? 1. class Mine extends Secret { 2. public static void main(String[] args) { 3. Mine m1 = new Mine(); m1.i = 5; 4. Mine m2 = new Mine(); m1.i = 5; 5. if (m1.equals(m2)) 6. System.out.println("YES"); 7. else 8. System.out.println("NO"); 9. } 10. } IT IS IMPOSSIBLE TO KNOW . The result of the call to equals() on line 5 depends on how the method was written in the Secret superclass. ------- This is a question from RHE. I have not understand the answer. Can anybody explain me the concept behind it. Thanks Padmini
Mikael Jonasson
Ranch Hand
Joined: May 16, 2001
Posts: 158
posted
0
Since you don't know anything about the Secret-class, you don't know if they have overridden/overloaded the equals() method. Consider this
In this case your program would print NO. In other cases it might print YES. It is, as you said, impossible to know. /Mike
Daniel Wu
Ranch Hand
Joined: Jun 13, 2001
Posts: 58
posted
0
Originally posted by padmini Babu: You are given the class file for a class called Secret. However, you do not have the source code or any information about the internals of the Secret class. You do know that the class has a protected int variable called i. What does the following application print out? 1. class Mine extends Secret { 2. public static void main(String[] args) { 3. Mine m1 = new Mine(); m1.i = 5; 4. Mine m2 = new Mine(); m1.i = 5; 5. if (m1.equals(m2)) 6. System.out.println("YES"); 7. else 8. System.out.println("NO"); 9. } 10. } IT IS IMPOSSIBLE TO KNOW . The result of the call to equals() on line 5 depends on how the method was written in the Secret superclass. ------- This is a question from RHE. I have not understand the answer. Can anybody explain me the concept behind it. Thanks Padmini
I am suspicious that you made an error when typing. Line 4 should be m2.i=5, not m1.1=5, right? I agree with you. we don't know yes or no unless we know whether Mine has overriden equals() method. if Mine doesn't, ans is NO. If Mine does correctly, ans is YES. Daniel
padmini Babu
Ranch Hand
Joined: Feb 10, 2001
Posts: 103
posted
0
Originally posted by Mikael Jonasson: [B]Since you don't know anything about the Secret-class, you don't know if they have overridden/overloaded the equals() method. protected boolean equals(Secret s) --------------------------------- Sorry Mike, I still have not understood. I am not very clear with the concept of overriding equals() . Can you give me more expln please. I do know that equals() method is always overridden because the equals() in object class is not very much helpful. But the signature of equals() in object class is: public boolean equals(Object obj) whereas in the secret class that you have provided, the signature is protected boolean equals(Secret s) but overridden methods cannot be made more restrictive This is what i think and I know. I still cannot figure out the concept behind my previous question. Please help me. xxxxxx Well Daniel, in the RHE tet, it is given as m1.i and not m2.i . So I don't know whether it should be m2.i and whether changing it to m2.i would make a difference. Thanks in advance. Padmini
padmini Babu
Ranch Hand
Joined: Feb 10, 2001
Posts: 103
posted
0
Hi ranchers, anybody hearing me? Scott, Cindy , Jane, help me please Thanks in advance Padmini
anand raman
Ranch Hand
Joined: Jun 06, 2001
Posts: 66
posted
0
Originally posted by padmini Babu: 3. Mine m1 = new Mine(); m1.i = 5; 4. Mine m2 = new Mine(); m1.i = 5;
Is this a typo .. U r setting the instance variable twice. Anand
Amit Madan
Ranch Hand
Joined: Dec 20, 2000
Posts: 32
posted
0
Hi here m1 & m2 are two different object & when we compair two object with equals it check object not state. so there m1 & m2 are different object hence always print "No". Amit