8 Polymorphic Abstractions
In the preceding chapters, we have defined a number of collection types in which all elements of the collection are int
s. Such collections are rather limiting. For example, suppose we wanted a set that contained Strings
. We would not be able to use our implementation of IntSet
for this purpose. Instead, we would need to provide a separate implementation for a StringSet
. And if we later discovered the need for a set of characters, we would need yet another new implementation.
Having to define a new version of a collection abstraction each time we need to store a different type of element is not very satisfactory. Instead, it would be better to define the collection type just once yet have it work for all types of elements. ...
Get Program Development in Java: Abstraction, Specification, and Object-Oriented Design now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.