I'm trying to understand the following code and a little bit confused. After some Googling, I found Anonymus Classes In OOP. But can't understand why in this code they declared Sorting object and calling an instance of MergeSort with sorting = new MergeSort();
? Can someone explain to me?
interface Sorting {
List sort(List list);
}
class MergeSort implements Sorting {
public List sort(List list) {
// sort implementation
return list;
}
}
class QuickSort implements Sorting {
public List sort(List list) {
// sort implementation
return list;
}
}
class DynamicDataSet {
Sorting sorting;
public DynamicDataSet() {
sorting = new MergeSort();
}
// DynamicDataSet implementation
}
class SnapshotDataSet {
Sorting sorting;
public SnapshotDataSet() {
sorting = new QuickSort();
}
// SnapshotDataSet implementation
}