This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B Bonus Exam, Comparable doubt

 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


You can use java.lang.Comaprable and java.util.Comparator to
sort collections whole elements are of any valid java type,
as long as all of the collection's elements are of the same class.


This is declared as wrong option.
The reason is given below:

It is incorrect because if a class is final and does not alreadt
implements Comparable, then Comparable can't be used to sort that
class.

I don't understand what is that talking about.

Please guide.

Thanks,
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chandra,

Its becuase..to sort elements of a class using Comparable, the implementation of the class has to be modified to include CompareTo() method.

So if the class doesnt implement Comparable and its final....than we can't add CompareTo() to it right...

So it can only be sorted using Comparator...as Comparator can be implemented by a external class rather than the class itself.

As the statement uses the word "and" its false...if it would have used "or" it will be true.

(I know I gave such an answer before about a statement using word "creates" ..so )I might not be right here tooo....
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Megha,

I got that!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic