• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

R & H Bonus1 question#36

 
Ranch Hand
Posts: 41
  • 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. }
A. Yes
B. No
C. Impossible to know
Correct answer is C. The result of the call to equals() on line 5 depends on how the method was written in the Secret superclass.
Can someone please explain?
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Parimala.
First off, I assume in line #4 you meant to say Mine m2 = new Mine(); m2.i = 5;.
The point here is, whoever programmed Secret.java could have made the requirements for equals() just about anything: without access to that code, you just don't know:
For instance, this version of Secret.java returns "NO" in Mine.java:

And this version of Secret.java returns "YES":

Therefore, the answer is indeterminate, and "C" is correct.
Art
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic