Thursday 13 December 2018

Open for extension and closed for modification




 For example, the Collections.sort method knows how to sort everything that implements the Comparable interface. This method is not limited to sorting just integers or just strings — it is not limited to any specific type. If you have a collection of objects that implement the Comparable interface, then you can sort it using the Collections.sort method. The sorting algorithm will work as it was designed, so we can say that it is closed to modification, but the sorting criteria will vary depending on the compareTo method implementation


The alogorithm is closed for modification, its merge sort used in Collection.sort(List<Comparable objects>).

But how your compare your two objects after implementing Comparable and overriding " int compareTo(Object o)" , is open for extension.

No comments:

Post a Comment