150 likes | 417 Views
ADT. data abstraction. Abstraction . representation of concepts by their relevant features only programming has two major categories of abstraction process abstraction (procedures) data abstraction (abstract data types -> objects). Process abstraction.
E N D
ADT data abstraction
Abstraction • representation of concepts by their relevant features only • programming has two major categories of abstraction • process abstraction (procedures) • data abstraction (abstract data types -> objects)
Process abstraction • external view: for caller of procedure – only the preconditions and effect of the process are relevant • internal view: for implementer of procedure – details of how to achieve effect starting from preconditions are important
Process abstraction • advantages • reusable procedures • revision of procedures independent of usage -> model for large programming projects – control of complexity, separation of tasks
Steps toward ADT • process abstraction inadequate – not enough organizing structure • modules • group related procedures (and data) together • compilation units • make groups as independent as possible (avoid recompilation – recall distinction of separate and independent compilation)
Steps toward ADT • encapsulation • independently compilable units with support for internal structure • visible interfaces for use by other units using them (types, procedure protocols, for error checking)
Abstract Data type (think Objects) • encapsulation for one data type (record?) and its operations e.g. complex number type with arithmetic ops • user program can create instances and apply operations • user program cannot manipulate (or even see) internal structure
Complex number ADT • ADT to represent and operate on complex numbers • behaviour - external • representation of a complex number • operations on complex number objects • implementation - internal • internal variables • methods code
Complex number ADT ADT Cpx • behaviour • representation of a complex number • string: “3 - 4i” or “(5,pi/4)” • operations on complex number objects • new(real, imaginary); new(magnitude, angle) • getReal, getImaginary, getMagnitude, getOrientation • add, subtract, multiply, divide, power • e.g., function add(Cpx x,Cpx y)returns Cpx
Complex number ADT ADT Cpx • implementation - internal • internal variables • double real • double im • methods code • function add(Cpx x,Cpx y)returns Cpx z = new Cpx(x.real+y.real, x.im+y.im) return z
User defined ADTs • language capability for programmer to create, compile and use new ADTs the same way as built-in ADTs • compile to make types, protocols visible with implementation invisible • any built-in/”inherited” features for all ADTs? e.g. copy, compare, assign • any restrictions on ADT types? simplicity/flexibility tradeoff e.g. parameterized ADTs, references only
Ada C++ Java Package Class Package and class Specification package (interface info for independent compilation) And Body package (implementation of procedures) p. 480 Class with privacy controls for information hiding NO higher level structure like package Friend functions with access to private structure Class with privacy controls And Package for organizing and mutual access EVERYTHING is ADTs (no independent methods/functions)
Ada C++ Java Package Class Package and class Contains (many) types and procedures NOT methods Parameterized e.g. collection package usable for different element types Template classes can be used with different types Instances of classes are allocated on run-time stack (may contain heap dynamic instance variables) Generic classes; Wrapper classes Object hierarchy provides same power (except for primitive types) Only references are on run-time stack – all instance objects on heap
Ada format • Specification package • Types • Procedure protocols • Some are ‘private’ – invisible to user programs • Body package • procedure code
Java abstraction concepts - ADTs and more --> objects • class • interface • abstract class • generic class • inheritance hierarchy • wrapper class