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

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -