Abstract Data Types and Encapsulation Concepts

Slides:



Advertisements
Similar presentations
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Advertisements

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.
ISBN Chapter 12 Support for Object-Oriented Programming.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Abstract data types & object-oriented paradigm. Abstraction Abstraction: a view of an entity that includes only the attributes of significance in a particular.
Lecture 9 Concepts of Programming Languages
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 12 Topics Introduction Object-Oriented Programming.
Chapter 11 Abstract Data Types and Encapsulation Concepts.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
(11.1) COEN Data Abstraction and OOP  Data Abstraction – Problems with subprogram abstraction – Encapsulation – Data abstraction – Language issues.
1 Chapter 11 Abstract Data Types and Encapsulation Constructs.
ISBN Chapter 12 Support for Object-Oriented Programming.
Chapter 11 and 12: Abstract Data Types and Encapsulation Constructs; Support for Object Orientation Lesson 11.
Chapter 11 Abstract Data Types and Encapsulation Concepts.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Learners Support Publications Classes and Objects.
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.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
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 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
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.
Names, Bindings, and Scope Session 3 Course : T Programming Language Concept Year : February 2011.
Chapter 12 Support for Object-Oriented Programming.
Object-Oriented Programming Chapter Chapter
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
ADT data abstraction. Abstraction  representation of concepts by their relevant features only  programming has two major categories of abstraction process.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
ISBN Object-Oriented Programming Chapter Chapter
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.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 11 Abstract Data Types and Encapsulation Concepts.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
ISBN Chapter 12 Support for Object-Oriented Programming.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
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 and Encapsulation Concepts
Support for Object-Oriented Programming
11.1 The Concept of Abstraction
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
Classes and Objects.
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 and Encapsulation Concepts Chapter 11 Abstract Data Types and Encapsulation Concepts

Chapter 11 Topics The Concept of Abstraction Introduction to Data Abstraction Design Issues for Abstract Data Types Language Examples Parameterized Abstract Data Types Encapsulation Constructs Naming Encapsulations Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

The Concept of Abstraction The concept of abstraction is fundamental in programming (and computer science) Nearly all programming languages support process abstraction with subprograms Nearly all programming languages designed since 1980 have supported data abstraction with some kind of module Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

Introduction to Data Abstraction Def: An abstract data type is a user-defined data type that satisfies the following two conditions: 1. The representation of, and operations on, objects of the type are defined in a single syntactic unit; also, other units can create objects of the type. 2. The representation of objects of the type is hidden from the program units that use these objects, so the only operations possible are those provided in the type's definition. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

Introduction to Data Abstraction Advantage of Restriction 1: Program organization, modifiability (everything associated with a data structure is together), and separate compilation Advantage of Restriction 2: Reliability--by hiding the data representations, user code cannot directly access objects of the type. User code cannot depend on the representation, allowing the representation to be changed without affecting user code. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

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 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

Design Issues Language Requirements to support abstract data types: 1. A syntactic unit in which to encapsulate the type definition. 2. A method of making type names and subprogram headers visible to clients, while hiding actual definitions. 3. Some primitive operations must be built into the language processor (usually just assignment and comparisons for equality and inequality) Some operations are commonly needed, but must be defined by the type designer e.g., iterators, constructors, destructors Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

Design Issues Language Design Issues: 1. Encapsulate a single type, or something more? 2. What types can be abstract? 3. Can abstract types be parameterized? 4. What access controls are provided? Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

Language Examples 1. Simula 67 Provided encapsulation, but no information hiding Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

Language Examples 2. Ada type NODE_TYPE is private; The encapsulation construct is the package Packages usually have two parts: 1. Specification package (the interface) 2. Body package (implementation of the entities named in the specification) Information Hiding 1. Hidden types are named in the spec package, as in: type NODE_TYPE is private; Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

Language Examples 2. Representation of an exported hidden type is specified in a special invisible (to clients) part of the spec package (the private clause), as in: package … is type NODE_TYPE is private; … private type NODE_TYPE is record end record; Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

Language Examples A spec package can also define unhidden types simply by providing the representation outside a private clause Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

Language Examples The reasons for the two-part type definition are: 1. The compiler must be able to see the representation after seeing only the spec package (the compiler can see the private clause) 2. Clients must see the type name, but not the representation (clients cannot see the private clause) Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

Language Examples Private types have built-in operations for assignment and comparison with = and /= Limited private types have no built-in operations Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

Language Examples 3. 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 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

Language Examples Information Hiding: Private clause for hidden entities Public clause for interface entities Protected clause - for inheritance (see Ch. 12) Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

Language Examples Constructors: Functions to initialize the data members of instances (they DO NOT create the objects) 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 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

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 (~) Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

Language Examples Friend functions or classes - to provide access to private members to some unrelated units or functions (NECESSARY in C++) Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

Language Examples Evaluation of C++ Support for Abstract Data Types Classes are similar to Ada packages for providing abstract data types Difference: packages are encapsulations, whereas classes are types Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

Language Examples Java Similar to C++, except: All user-defined types are classes All objects are allocated from the heap and accessed through reference variables Individual entities in classes have access control modifiers (private or public), rather than clauses Java has a second scoping mechanism, package scope, which can be used in place of friends All entities in all classes in a package that do not have access control modifiers are visible throughout the package Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

Language Examples C# Based on C++ and Java Adds two access modifiers, internal and protected internal All class instances are heap dynamic Default constructors are available for all classes Garbage collection is used for most heap objects, so destructors are rarely used structs are lightweight classes that do not support inheritance Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

Language Examples C# (continued) Common solution to need for access to data members: accessor methods (getter and setter) C# provides properties as a way of implementing getters and setters without requiring explicit method calls Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

C# Property Example public class Weather { public int DegreeDays { //** DegreeDays is a property get { return degreeDays; } set { degreeDays = value; private int degreeDays; ... Weather w = new Weather(); int degreeDaysToday, oldDegreeDays; w.DegreeDays = degreeDaysToday; oldDegreeDays = w.DegreeDays; Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

Parameterized Abstract Data Types 1. Ada Generic Packages Make the stack type more flexible by making the element type and the size of the stack generic Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

Example Program package INT_STACK is new GENERIC_STACK(100, INTEGER); package FLOAT_STACK is new GENERIC_STACK(500, FLOAT); Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

Parameterized Abstract Data Types 2. 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); Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

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 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

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 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

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 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

Encapsulation Constructs Ada Package Can include any number of data and subprogram delcarations Two parts: specification and body Can be compiled separately C# Assembly Collection of files that appears to be a single dynamic link library or executable Larger construct than class; used by all .NET programming languages Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

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 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

Naming Encapsulations Java Packages Packages can contain more than one class definition; classes in a package are partial friends Clients of a package can use fully qualified name or use the import declaration Ada Packages Packages are defined in hierarchies which correspond to file hierarchies Visibility from a program unit is gained with the with clause Copyright © 2004 Pearson Addison-Wesley. All rights reserved.