PPT Slide
Implementation of a Generic Sorting Algorithm using interfaces.
Let us look at another example code in detail. This is an example where we cannot use an abstract class.
The class Tile models tiled windows on a screen desktop. Tiled windows are rectanges plus a z-order variable which governs the display order. Tiles with a larger z-order are displayed in front of those with a smaller z-order. We will need to keep the instances of Tile in sorted order by z-order.
The choices for keeping the instances of Tile in sorted order include:
1. Implementation of a sort algorithm in the Tile class
2. Have the Tile class inherit from one of the many Java classes which implement sort algorithms.
Number 2 sounds like much less work!
The Tile class inherits from the Rectangle class of the awt package. Therefore we cannot have it inherit from the abstract class Sortable. So Number 2 is not possible.
But interfaces solve this problem.