Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in equals()

 
Sajeena Binoj
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rick.
 
deshdeep divakar
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
deshdeep divakar
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
deshdeep divakar
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seenu
Thanks well that's a blunder I will be more
alert from now thanks one again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic