Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in equals() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in equals()" Watch "Doubt in equals()" New topic
Author

Doubt in equals()

Sajeena Binoj
Greenhorn

Joined: Jul 29, 2005
Posts: 6
This is a question from Marcus's Tiger mock exam

What will happen when you attempt to compile and run the following code?

--------------------------------------------------------------------
Select 1 option


1) Compile time error, an abstract class cannot have non abstract methods.
2) Compile time error, the equals method of the String class takes a String parameter not a StringBuffer
3) Compile time error, class Ilea does not implement the getLeader method of Lcc
4) Compilation and output of "equals"
5) Compilation, but no output at runtime

------------------------------------------------------------------------

The answer is 5th option(Compilation, but no output at runtime).
I compiled the same and found that the answer is correct.

Can anyone please explain this answer?

Thanks in advance
Sajeena.

[ August 03, 2005: Message edited by: Sajeena Binoj ]
[ August 03, 2005: Message edited by: Barry Gaunt ]
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
Sure, there's nothing wrong with any of the code and so it compiles and runs and the comparison fails. A StringBuffer will never equal a String, no matter what the content of each is. That's part of the canonical equals() contract.
Sajeena Binoj
Greenhorn

Joined: Jul 29, 2005
Posts: 6
Thanks Rick.
deshdeep divakar
Ranch Hand

Joined: Apr 19, 2004
Posts: 91
Hello Rick,

do'nt you think the answer should be compilation error
also coz public method getLeader() is not implemented in
concrete class which is must.

Thanks & Regards
Java Man
Deshdeep


"Do not be afraid of going slow, be afraid of standing still"
deshdeep divakar
Ranch Hand

Joined: Apr 19, 2004
Posts: 91
Hello Rick,

do'nt you think the answer should be compilation error
also coz public method getLeader() is not implemented in
concrete class which is must.

Thanks & Regards
Java Man
Deshdeep
Seenu Reddy
Ranch Hand

Joined: Apr 03, 2002
Posts: 108
deshdeep,

the getLeader() method in class LCC is not abstract method. so its not must to override the same. if the method is declared abstact, then it must override the method in the sub class.

hope you understood.

Thanks,
Seenu


Thanks,<br />Seenu<br />SCJP1.4,SCBCD1.3,Next-->SCWCD1.4
deshdeep divakar
Ranch Hand

Joined: Apr 19, 2004
Posts: 91
Seenu
Thanks well that's a blunder I will be more
alert from now thanks one again.
 
Don't get me started about those stupid light bulbs.
 
subject: Doubt in equals()
 
Similar Threads
Abstract Class and methods
Explanation required for equals method in String class
OK, I know String's equal takes StringBuffer object, but when are they really equal?
Abstract Class
equals() method comparing String and StringBuffer objects.