Abstract data types & object-oriented paradigm. Abstraction Abstraction: a view of an entity that includes only the attributes of significance in a particular.

Slides:



Advertisements
Similar presentations
MT311 (Oct 2007) Java Application Development Object-Oriented Programming Languages Tutorial 8.
Advertisements

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
C++ Programming Languages
Chapter 11 Abstract Data Types and Encapsulation Concepts.
ICE1341 Programming Languages Spring 2005 Lecture #18 Lecture #18 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
CPS 506 Comparative Programming Languages Abstract Data Type and Encapsulation.
CS2403 Programming Languages Abstract Data Types and Encapsulation Concepts Chung-Ta King Department of Computer Science National Tsing Hua University.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
A Brief Introduction to Java Programming the World Wide Web.
Lecture 9 Concepts of Programming Languages
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Abstract Data Types and Encapsulation Concepts
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
OOP Languages: Java vs C++
Chapter 11 Abstract Data Types and Encapsulation Concepts.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Programming Languages and Paradigms Object-Oriented Programming.
1 Chapter 11 Abstract Data Types and Encapsulation Constructs.
Chapter 11 Abstract Data Types and Encapsulation Concepts.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Programming Languages and Paradigms Object-Oriented Programming.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
Chapter 11 Abstract Data Types and Encapsulation Concepts.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
1 Abstract Data Types & Object Orientation Abstract Data Types (ADT) Concepts –Data Abstraction –ADT in PLs –Encapsulation Object Orientation –Principal.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Chapter 10, Slide 1 ABSTRACT DATA TYPES Based on the fundamental concept of ABSTRACTION:  process abstraction  data abstraction Both provide:  information.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Object-Oriented Programming Chapter Chapter
ADT data abstraction. Abstraction  representation of concepts by their relevant features only  programming has two major categories of abstraction process.
ISBN Object-Oriented Programming Chapter Chapter
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
1 CS Programming Languages Class 22 November 14, 2000.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Chapter 11 Abstract Data Types and Encapsulation Concepts.
ISBN Chapter 12 Support for Object-Oriented Programming.
Chapter 11: Abstract Data Types Lecture # 17. Chapter 11 Topics The Concept of Abstraction Advantages of Abstract Data Types Design Issues for Abstract.
Abstract Data Types and Encapsulation Concepts
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Abstract Data Types and Encapsulation Concepts
ABSTRACT DATA TYPES Based on the fundamental concept of ABSTRACTION:
11.1 The Concept of Abstraction
Abstract Data Types Objects are one way to implement ADTs
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Abstract Data Types and Encapsulation Concepts
Abstract Data Types and Encapsulation Concepts
Abstract Data Types and Encapsulation Concepts
Chapter 11 Abstract Data Types and Encapsulation Concepts
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
F II 3. Classes and Objects Objectives
Abstract Data Types and Encapsulation Concepts
Abstract Data Types and Encapsulation Concepts
Lecture 10 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Abstract Data Types and Encapsulation Concepts
11.1 The Concept of Abstraction
Lecture 9 Concepts of Programming Languages
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

Abstract data types & object-oriented paradigm

Abstraction Abstraction: a view of an entity that includes only the attributes of significance in a particular context. Two fundamental abstractions: process abstraction & data abstraction Process abstraction: sortInt(list, list_len) –sorting algorithms is not revealed –user code remains same even implementation of sorting algorithm is changed –used for a long time

Abstract data type Abstract Data Type: an encapsulation that includes only the data representation and the subprograms that provides operations for that type. –reliability: user cannot directly access objects –readability: without implementation details

Introduction to Data Abstraction Built-in types are abstract data types e.g. int type in Java –The representation is hidden –Operations are all built-in –User programs can define objects of int type User-defined abstract data types must have the same characteristics as built-in abstract data types –To declare variables of that type –A set of ops

Abstract data type example interfaces: create(stack) destroy(stack) empty(stack) push(stack, item) pop(stack) top(stack) code example: … create(stk1); push(stk1, 34); push(stk1, 20); if ( !empty(stk1) ) temp = top(stk1); stack implementation can be changed from array to list w/o affecting the code

Encapsulation Encapsulation: a grouping of subprograms and the data they manipulate Hiding the implementation details of the abstract interface Protect internal data Reliability, maintenance

Language Examples-C++ Based on C struct type and Simula 67 classes The class is the encapsulation device All of the class instances of a class share a single copy of the member functions Each instance of a class has its own copy of the class data members Instances can be static, stack dynamic, or heap dynamic

At first glance #include class stack { private: int *stackPtr; int maxLen; int topPtr; public: stack() { stackPtr = new int [100]; maxLen = 99; topPtr = -1; } ~stack() { delete [ ] stackPtr; } void push(int item) { if (topPtr == maxLen) cerr << “Stack full\n”; else stackPtr[++topPtr] = item; } void pop() { if (topPtr == -1) cerr << “Stack empty\n”; else topPtr--; } int top() { return ( stackPtr[topPtr] ); } int empty() { return ( topPtr == -1); } } // end class stack Data members Member functions

C++ Class data members: data defined in a class –*stack_ptr, max_len, top_ptr member functions: functions defined in a class –also called access interfaces –push( ), pop( ), top( ), empty( ) private: entities (data or member functions) that are to be hidden from outside the class public: entities (data or member functions) that are visible outside the class

C++ class (con’t) constructor: initialize the data members of newly created objects –stack( ) –implicitly called when an object of the class type is created –can have one or more constructor for a class destructor: implicitly called when the lifetime of an instance of the class ends –~stack( )

Example in C++ #include class stack { private: int *stackPtr; int maxLen; int topPtr; public: stack() { stackPtr = new int [100]; maxLen = 99; topPtr = -1; } ~stack() { delete [ ] stackPtr; } void push(int item) { if (topPtr == maxLen) cerr << “Stack full\n”; else stackPtr[++topPtr] = item; } void pop() { if (topPtr == -1) cerr << “Stack empty\n”; else topPtr--; } int top() { return ( stackPtr[topPtr] ); } int empty() { return ( topPtr == -1); } } // end class stack

Class usage in C++ Code example in C++: void main() { int topOne; stack stk; // stack-dynamic stk.push(42); stk.push(20); topOne = stk.top(); stk.pop(); … // stk being freed } stack stk  constructor is called to initialize the instance at the end of the main, stk’s destructor is called

Language Examples Constructors: –Functions to initialize the data members of instances –May also allocate storage if part of the object is heap-dynamic –Can include parameters to provide parameterization of the objects –Implicitly called when an instance is created –Can be explicitly called –Name is the same as the class name

Language Examples Destructors –Functions to cleanup after an instance is destroyed; usually just to reclaim heap storage –Implicitly called when the object ’ s lifetime ends –Can be explicitly called –Name is the class name, preceded by a tilde (~)

Examples in Java Code example in C++: void main() { int topOne; stack stk; // stack-dynamic stk.push(42); stk.push(20); topOne = stk.top(); stk.pop(); … } Code example in Java: public class TestStack { public static void main(…) { INTEGER topOne; StackClass myStack = new StackClass(); myStack.push(42); myStack.push(20); topOne = myStack.top(); myStack.pop(); … } Java uses implicit garbage collection (no destructor) and reference variable (not pointer)

Java features Stack_class does not have destructor –implicit garbage collector In main( ), myStack does not get freed –implicit garbage collector The use of reference variables –Java does not support pointer

Example:Stack in Java import java.io.*; class Stack_class { private int [ ] stack_ref; private int max_len, top_index; public Stack_class( ) { stack_ref = new int [100]; max_len = 99; top_index = -1; } public void push (int number) { if (top_index = max_len) System.out.println( “Error stack full”); else stack_ref[++top_index] = number; } public void pop( ) { if (top_index == -1) System.out.println( “Error Stack empty”); else –top_index; } public int top( ) { return (stack_ref[top_index]); } public boolean empty( ) { return (top_index == -1); } } // end class stack

Parameterized Abstract Data Types- C++ Templated Classes –Classes can be somewhat generic by writing parameterized constructor functions, e.g. stack (int size) { stk_ptr = new int [size]; max_len = size - 1; top = -1; } stack stk(100);

Parameterized Abstract Data Types The stack element type can be parameterized by making the class a templated class Java does not support generic abstract data types

Parameterized Abstract Data in C++ #include class stack { private: int *stackPtr; int maxLen; int topPtr; public: stack(int size) { stackPtr = new int [size]; maxLen = size -1; topPtr = -1; } ~stack() { delete [ ] stackPtr; } void push(int item) { if (topPtr == maxLen) cerr << “Stack full\n”; else stackPtr[++topPtr] = item; } void pop() { if (topPtr == -1) cerr << “Stack empty\n”; else topPtr--; } int top() { return ( stack[topPtr] ); } int empty() { return ( topPtr == -1); } } // end class stack Usage: stack myStack(50);

Template abstract data type in C++ #include template class stack { private: Type *stackPtr; public: stack() : stackPtr (new Type [100]), maxLen (99), topPtr(-1) { } stack(int size) { stackPtr = new Type [size]; maxLen = size – 1; topPtr= -1; } C++ example in left stack stk; stack stk(150); C++ template classes are instantiated at compile time –code for new type is created when not existed Java does not support generic abstract data type as in left

Encapsulation Constructs Original motivation: –Large programs have two special needs: 1. Some means of organization, other than simply division into subprograms 2. Some means of partial compilation (compilation units that are smaller than the whole program) Obvious solution: a grouping of subprograms that are logically related into a unit that can be separately compiled (compilation units) –These are called encapsulations

Encapsulation Constructs Nested subprograms in Ada and Fortran 95 Encapsulation in C –Files containing one or more subprograms can be independently compiled –The interface is placed in a header file –Problem: the linker does not check types between a header and associated implementation Encapsulation in C++ –Similar to C –Addition of friend functions that have access to private members of the friend class

Naming Encapsulations Large programs define many global names; need a way to divide into logical groupings A naming encapsulation is used to create a new scope for names C++ Namespaces –Can place each library in its own namespace and qualify names used outside with the namespace –C# also includes namespaces

OO Key feature Abstraction –Well-defined interface Hierarchy –Composition –Derivation –inheritance Encapsulation –Hiding the detail Polymorphism

From C to C++ Without abstraction main(){ int stack_items[STACKSIZE], stack_top =0, x; /*push x into stack*/ stack_item[stack_top++] = x; /*pop stack to x*/ x = stack_item[--stack_top]; }

Abstraction Abstraction: void init(stack *s; viod push(stack *s, int i); int pop(stack *s); void cleanup(stack *s); typedef struct{ int items[stacksize]; Int top } stack; Void init(stack *s) {s->top = 0;} Void push (stack *s, int i) {s-> items[s->top++] =I;} Int pop(stack *s){return s->items[-- s->top];} … main(){ int x; stack stack1; init(&stack1); push(&stack1, x); … }