Docsity

Data Abstraction & Function in CMSC 433 - Programming Language Technologies - Prof. Atif M, Study notes of Programming Languages

Toggle sidebar
bg1
CMSC 433 – Programming Language
Technologies and Paradigms
Spring 2007
Abstraction
Feb. 27, 2007
Data Abstraction
• Data abstraction = objects + operations
– List + { addFirst, addLast, removeFirst, ... }
– Set + { add, contains, ...}
• Categories of operations
– Constructors (creators/producers)
– Mutators/Modifiers
–Observers
Abstraction Function
• Specification for data structure is abstract
• Implementation of data structure is concrete
• How do you know if implementation meets the
spec?
• Abstraction function : concrete abstract
– Specifies how the representation of an abstract data
type is interpreted as an abstract value.
Example
class IntSet { int[] elts; ... }
– AF(s) = { s.elts[i] | 0 <= i <= elts.length }
• You always need an abstraction function when
you build a data abstraction
– Often it’s implicit
pf2

Partial preview of the text

Download Data Abstraction & Function in CMSC 433 - Programming Language Technologies - Prof. Atif M and more Study notes Programming Languages in PDF only on Docsity!

CMSC 433 – Programming Language

Technologies and Paradigms

Spring 2007

Feb. 27, 2007Abstraction

Data Abstraction

  • Set + { add, contains, ...}– List + { addFirst, addLast, removeFirst, ... }Data abstraction = objects + operations
  • Observers– Mutators/Modifiers– Constructors (creators/producers)Categories of operations

Abstraction Function

Specification for data structure is abstract

Implementation of data structure is concrete

spec?How do you know if implementation meets the

Abstraction function : concrete abstract

  • Specifies how the representation of an abstract data type is interpreted as an abstract value.

Example

class (^) IntSet (^) { int[] elts; ... (^) }

  • AF(s) = { s.elts[i] | 0 <= i <= elts.length }
  • Often it’s implicityou build a data abstractionYou always need an abstraction function when

Representation Invariant(s)

abstract data type is well formed,A constraint that characterizes whether an instance of an

  • Constraint must hold class IntSet { –^ Before and after each operation^ After the constructor has finished // rep (^) inv: elts contains (^) no duplicates int[] (^) elts; (^) ... }

Part of the (internal) specification

Implementing the Rep Invariant

public Interesting idea: Write a function to check the rep (^) boolean repOK() (^) { ...check (^) for duplicates in (^) elts... }

  • Can call during unit testing– Can add wherever you expect rep to holdWhere can you use this?

Cost?