Advantages of Abstract Data Types
Abstract data types offer several advantages over concrete data types:
- Representation Independence: Most of the program becomes independent of the abstract data type's representation, so that representation can be improved without breaking the entire program.
- Modularity: With representation independence, the different parts of a program become less dependent on other parts and on how those other parts are implemented.
- Interchangeability of Parts: Different implementations of an abstract data type may have different performance characteristics. With abstract data types, it becomes easier for each part of a program to use an implementation of its data types that will be more efficient for that particular part of the program.
Example:
Java's standard libraries supply several different implementations of its
Map
data type. TheTreeMap
implementation might be more efficient when a total ordering on the keys can be computed quickly but a good hash value is hard to compute efficiently. TheHashMap
implementation might be more efficient when hash values can be computed quickly and there is no obvious ordering on keys. The part of a program that creates aMap
can decide which implementation to use. The parts of a program that deal with a createdMap
don't have to know how it was implemented; once created, it's just aMap
.If it weren't for abstract data types, every part of the program that uses a
Map
would have to be written twice, with one version to deal withTreeMap
implementations and another version to deal withHashMap
implementations.
These advantages become more important in larger programs, so they often go under-appreciated by programmers with limited experience.