java - Why implement Comparable interface when you can define compareTo method in a class? -
this question has answer here:
you can define compareto
method in class without implementing comparable
interface. benefits of implementing comparable
interface?
the benefit of implementing interface methods require object implements comparable
interface. gives them guarantee object you're passing has compareto
method correct signature.
there's no way in java have method require object implement given method (such compareto
) in , of itself. around this, interfaces created. time have object know comparable
, know can call compareto
on it.
Comments
Post a Comment