Abstract Data Types (ADT)
- For many of you, your first programming language was an Object-oriented language.
- The basic idea of objects and classes can be abstracted to the concept of an Abstract Data Type (ADT)
- What is “abstraction”
- Abstraction is a way of looking at some entity that only focuses on the most significant parts.
- Two main types of abstraction:
- Process abstraction (i.e. functions)
- Data abstraction / composite data (e.g.,
struct
s in C.)
- ADT encloses both the data and the subprograms that operate on that data.
- (Doesn’t that sound like the way classes were first described?)
- Access control is a key feature of ADTs
- Hides information that the client (i.e., “user”) doesn’t need to know.
- Prevent access to underlying data in an uncontrolled manner.
- Separates interface from implementation.
- Why is this helpful?
- Allows changes/improvements to the implementation without breaking existing code.
- Reduces the chances of internal data being put in an unanticipated state.
- Imagine directly setting an Array’s length to a negative number.
- When, how often should code check for this possibility?
- What characteristic does this describe?
- Consider using
structs
to program in an “OO style” in C.
- What’s missing?
- Protection against changing values in the
struct
without checking for validity.
- In theory, and ADT’s data should be accessed through functions only. But, many OO languages allow public instance variables.
- Why?
- What language qualities does this affect?
- What about instance variables that are read-only? What problems does this solve? What problems remain?
- Improves writaility (less to write)
- But, then changing the way data is stored might break existing code.
- What are the main benefits of ADTs?
- They reduce complexity. We can focus on the relevant details and forget the less important details.
- They provide a namespace to help reduce name collisions.
- Consider this: In some sense all built-in types (e.g., float) are ADTs.
- How so?
- We don’t (necessarily) have direct access to the individual bits of the float.
- We don’t need to know how the float is stored, we simply have a set of operations (+, -, *, /, etc.) that manipulate the float.
- If the representation were to change, very few programs would break (only the exceedingly few that perform individual bit manipulations).
- What features does a language need to support ADTs?
- A way to declare the type (i.e., the bundled component data )
- A way to expose what should be public and hide what should be private.
- A way to provide external visibility for the name and interface .
- Other choices
- Parameterization — e.g., templates in C++ and generics in Java.
- How access control is specified.
- Which is better C++ “block” style public/private or Java “per-method” style?
- Whether interface and implementation are specified together (e.g., Java, Ruby, JavaScript) or separately (C++)
- Encapsulation: Many languages also provide some sort of “package” or “module” system to avoid name collisions among ADTs.
- “Friends”. Think about multiplying a vector and a matrix
- How does C++ give access to the internals of both?
- How does Java?