Abstraction
255
© Dr Markus Lumpe, 2021
New Sets of Values
• The definition of a new data type (i.e., a new set of values) consists of two ingredients:
Some set, called the interface, that serves as
•
representation of the newly define data type, and
Some set of procedures, called the implementation,
•
that provides the operations, which can be used to manipulate the newly defined data type.
256
© Dr Markus Lumpe, 2021
Representation Independence
• The representation of new data types can be often very complex.
• When working with new data types, we usually do not want to be concerned with their actual representation. In fact, program become more reliable and robust, if they do not depend on the actual representation of data type. Data types that do not expose their actual representation are called representation transparent.
•Data types in C/C++ are in general not representation transparent (e.g. the size of integers in C/C++ is platform dependent).
•Data types in Java are basically representation transparent (arrays are an exception, since they are represented by objects).
257
© Dr Markus Lumpe, 2021
Opaque Representation
• A data type is opaque if there is no way to find out its representation, even by printing.
SinglyLinkedList is not opaque as we have unrestricted access
to fData and fNext!
258
© Dr Markus Lumpe, 2021
Object-Oriented Encapsulation
• Object-oriented encapsulation is a principle that provides the means to obtain an opaque data type:
• All instance variables have private visibility. • All member functions have public visibility.
• The extent to which this scheme is used can differ!
• Opaque here does not necessarily mean that you cannot see the implementation, just that there are no pragmatic approaches to exploit this knowledge.
259
© Dr Markus Lumpe, 2021
Pros & Cons
• Opaque data types enforce the use of defining procedure (i.e., a constructor).
• Opaque data types are more secure. Access to values of opaque data types is only possible by means of access procedures defined in an interface.
• Transparent data types are easier to debug and to extend.
• The fact that transparent data types expose their internal representation is also a disadvantage (limited security).
260
© Dr Markus Lumpe, 2021
Abstract Data Type
•The technique used to define new data types independently of their actual representation is called data abstraction.
• A data type, which has been defined in this way is called abstract data type. A client (program) can use values of an abstract data type by means of the interface without knowing their actual representation (which can change over time).
261
© Dr Markus Lumpe, 2021
Representation Strategies for ADTs
Abstract Data Type Representation 1 Representation N
• Given an interface for a data type we can change the underlying representation if needed using different strategies.
262
© Dr Markus Lumpe, 2021
Data Abstraction
• Data abstraction enforces representation independence.
• Data abstraction divides the data types in interfaces and implementations:
• Interfaces are used to specify the set of values the data types represents, the operations, which are available for that data type, and properties these operations may be guaranteed to have.
• Implementations provide a specific representation of the data and code for the operations.
263
© Dr Markus Lumpe, 2021
Examples of Abstract Data Types
• Files
• Lists, hash tables, vectors, bags
• Strings, records, arrays
• Objects with private instance variables and public methods
•Standardized integers (e.g. in Java the type int is represented using 32 bits and big endian format, network byte order, on every platform)
264
© Dr Markus Lumpe, 2021
Constructors and Access Procedures
• In order to create, manipulate, and verify that a given value is of the desired data type, we need the following ingredients:
• Constructors that allow us to build values of a given data type,
• A predicate that tests whether a given value is a representation
of a particular data type, and
• Some access procedures that allow us to extract a particular information from a given representation of a data type.
265
© Dr Markus Lumpe, 2021
Can we represent lists as abstract data type?
266
© Dr Markus Lumpe, 2021
r-value variant
267
© Dr Markus Lumpe, 2021
List Test
268
© Dr Markus Lumpe, 2021
The type list
269
© Dr Markus Lumpe, 2021