Data Abstraction

Programs should make as few assumptions about the data as possible.

Data abstraction is a powerful concept in computer science that allows programmers to treat code as objects -- for example, car objects, chair objects, people objects, etc. That way, programmers don't have to worry about how code is implemented -- they just have to know what it does.

Data abstraction mimics how we think about the world. When you want to drive a car, you don't need to know how the engine was built or what kind of material the tires are made of. You just have to know how to turn the wheel and press the gas pedal.

An abstract data type consists of two types of functions:

  • Constructors: functions that build the abstract data type.

  • Selectors: functions that retrieve information from the data type.

Programmers design ADTs to abstract away how information is stored and calculated such that the end user does not need to know how constructors and selectors are implemented. The nature of abstract data types allows whoever uses them to assume that the functions have been written correctly and work as described.

Manipulate compound values as units

Compound values

  • combine different values together:

    • a date: a year, a month, a day

    • a position: latitude and longitude - a compound data value that our program can manipulate as a single unit, but also has 2 parts that can be considered individually.

Isolate 2 parts of any program that uses data

  • How data are represented?

    • rational numbers:

      • numerator / denominator

      • exact representation as fractions, as soon as division occurs, the exact representation may be lost!

  • How data are manipulated?

    • compose rational numbers:

      • constructor - rational (n, d) -> rational nr. x

      • numer(x) -> numerator x

      • denom(x) -> denominator x

Last updated