• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Rhe question -- identifiers

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,
anybody hearing me? Scott, Cindy , Jane, help me please
Thanks in advance
Padmini
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic