Presentation is loading. Please wait.

Presentation is loading. Please wait.

(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)

Similar presentations


Presentation on theme: "(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)"— Presentation transcript:

1 (1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)

2 (2) Abstraction In general: A view or representation of an entity that includes only the attributes of significance in a particular context In computer science: Separation of specification from implementation A weapon against the complexity of programming

3 (3) Information Hiding Why hide information from yourself and other programmers? “You don’t want to know” But also … “If I told you I’d have to kill you!” Not quite, but … “A little knowledge is a dangerous thing”

4 (4) Types of Abstraction Process Abstraction Saying that a process will be done without saying how As old as the first programming languages: functions and subprograms Data Abstraction Saying that data has certain properties without saying how it is represented Properties are specified in terms of operations, which are process abstractions

5 (5) Encapsulation Motivations: Large programs hard to understand without modularization Cost of recompilation becomes high Encapsulation into Modules solves both these problems Syntactic units that group together data and the operations that apply to them When done right, modules - are compilation units and - provide an abstract logical organization

6 (6) More on Modules Information Hiding is key to abstraction Small modules easier to Write Test Understand Maintain Reuse Small change in specification should mean small change in implementation

7 (7) Evolution of Encapsulation LISP Functions provide procedural abstraction No particular organization ALGOL Organization by nested subprograms Not independent of each other C Separately compiled files Type checking across files not done Modern languages Modules that compiled separately with type checking Fully abstract data types Objects (next chapter)

8 (8) Abstract Data Type A collection of data and a set of operations on that data Encapsulated: The representation of the type and the operations are in a single syntactic unit Information Hiding: The representation is hidden from client programs and only the ADT operations may be used on them Defined in terms of how (abstracted) operations change the logical properties of the data Implemented as a data structure and operations ADT ≠ data structure

9 (9) Specifying ADTS Pseudocode Structured natural language Informal semantics Axioms Formal semantics: prove properties No direct connection to code Java interface Formal parameters (but no semantics) Supports coding

10 (10) ADT List Example createList() - create an empty list isEmpty() - determine whether a list is empty add(index,item) - add an item at a given position in the list remove(index) - remove the item at a given position in the list removeAll() - remove all items from the list get(index) - retrieve the item at a given position

11 (11) ADT List Axioms 1. (aList.createList()).size() == 0 2. (aList.add(i,x)).size() == aList.size() + 1 3. (aList.remove(i)).size() == aList.size() -1 4. (aList.createList()).isEmpty() == true 5. (aList.add(i,item)).isEmpty() == false 6. (aList.createList()).remove(i) = error 7. (aList.add(i,x)).remove(i) = aList 8. (aList.createList()).get(i) == error 9. (aList.add(i,x)).get(i) == x 10. aList.get(i) == (aList.add(i,x)).get(i+1) 11. aList.get(i+1) == (aList.remove(i)).get(i) Prove: ((aList.add(1,b)).add(1,a)).get(2) = b

12 (12) ADT List Implementations Adjacency Representation (Array) Linked Representation Can change representation without changing client code

13 (13) Design Issues Language should provide Syntactic unit that encapsulates definitions Header visible to clients Body hidden from clients Which operations are pre-provided? Assignment, comparison? Constructors, destructors? Iterator? Can the type be parameterized?

14 (14) SIMULA 67 SIMULA 67 contributed encapsulation via classes class class_name; begin class variable definitions class subprogram definitions class code end class_name; Lacked information hiding

15 (15) ADA Contributed information hiding Encapsulated with packages Specification package type NODE_TYPE is private; Body package package LINKED_LIST_TYPE is; type NODE_TYPE is private; private... type NODE_TYPE is record INFO: INTEGER; LINK : PTR; end record

16 (16) ADA Using a Package with imports packages use eliminates need for package name qualifiers with LINKED_LIST_TYPE; use LINKED_LIST_TYPE; Generic (parameterized) ADTS Example: enable linked list to store other than integers generic type ELEMENT_TYPE is private; package GENERIC_LINKED_LIST is … … ELEMENT_TYPE … // somewhere in the body Explicit Instantiation: package INTEGER_LINKED_LIST is new GENERIC_LINKED_LIST(INTEGER);

17 (17) C++ Uses Classes (rather than Packages) Classes are types Extension of C struct Member functions shared by all instances - Can be defined to be inlined - Recompilation issue Data members usually unique to instances Class instances are stack dynamic - But can include heap-dynamic members - Explicit management with new, delete

18 (18) C++ Example Public and Private Clauses Constructor and Destructor Functions class stack { private: … private members … public: stack() { … } // constructor ~stack() { … } // destructor void push(int val) { … } void pop() { … } int top() { … } int empty() { … } }

19 (19) C++ Generic ADTs Templated Classes template class stack { private: Type *stack_ptr; … more private members … public: stack() { … } // constructor ~stack() { … } // destructor void push(Type val) { … } void pop() { … } int top() { … } int empty() { … } } Implicit Instantiation: created when object that needs a new version is created

20 (20) C++ Evaluation Unlike packages, classes are types (can make instances, while you just use a package) Limitation of classes: without generalized encapsulation must choose which object to associate an operation Example: Multiplication of Matrix and Vector Friend construct makes member function available to multiple classes friend Vector multiple(const Matrix&, const Vector&); ADA solves this by putting Matrix and Vector in the same package Java does both … Provides also for OOP (next chapter)

21 (21) Java Most similar to C++. Main differences: All user defined types are classes (as well as all classes are types) Subprograms can only be defined as methods (member functions) Packages provide higher level encapsulations, and are hierarchical Public, private, protected, package scope Heap dynamic, implicit destruction

22 (22) Java Example Public and Private are modifiers, not clauses class MyStack { private int [] stackRef; private int max_len, topIndex; public myStack () { … } // constructor public void push(int val) { … } public void pop() { … } public int top() { … } public boolean empty() { … } } Parameterized ADTs handled via Abstract Classes

23 (23) Wednesday: OOP and examples with Python, Java


Download ppt "(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)"

Similar presentations


Ads by Google