SlideShare a Scribd company logo
1 of 7
ICS 313 - Fundamentals of Programming Languages 1
11. Abstract Data Types
11.1 The Concept of Abstraction
The concept of abstraction is fundamental in
programming
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
ICS 313 - Fundamentals of Programming Languages 2
11.2 Encapsulation
Original motivation:
Large programs have two special needs:
Some means of organization, other than simply division into subprograms
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
Examples of Encapsulation Mechanisms
Nested subprograms in some ALGOL-like languages (e.g., Pascal)
FORTRAN 77 and C - Files containing one or more subprograms can
be independently compiled
FORTRAN 90 and Ada - separately compilable modules
11.3 Introduction to Data Abstraction
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.
ICS 313 - Fundamentals of Programming Languages 3
11.3 Introduction to Data Abstraction (continued)
Advantage of Restriction 1:
Same as those for encapsulation: 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
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
11.4 Design Issues
Language Requirements to support abstract data types:
A syntactic unit in which to encapsulate the type definition.
A method of making type names and subprogram headers
visible to clients, while hiding actual definitions.
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
Language Design Issues:
Encapsulate a single type, or something more?
What types can be abstract?
Can abstract types be parameterized?
What access controls are provided?
ICS 313 - Fundamentals of Programming Languages 4
11.5 Language Examples
Simula 67
Provided encapsulation, but no information hiding
Ada
The encapsulation construct is the package
Packages usually have two parts:
Specification package (the interface)
Body package (implementation of the entities named in the specification
Information Hiding
Hidden types are named in the spec package, as in:
type NODE_TYPE is private;
11.5 Language Examples (continued)
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;
…
A spec package can also define unhidden types simply by providing the
representation outside a private clause
The reasons for the two-part type definition are:
The compiler must be able to see the representation after seeing only the spec
package (the compiler can see the private clause)
Clients must see the type name, but not the representation (clients cannot see the
private clause)
ICS 313 - Fundamentals of Programming Languages 5
11.5 Language Examples (continued)
Private types have built-in operations for assignment
and comparison with = and /=
Limited private types have no built-in operations
---> SHOW specification and body packages
(pp. 443-444) and the procedure that uses
them (p. 445)
11.5 Language Examples (continued)
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
Information Hiding:
Private clause for hidden entities
Public clause for interface entities
Protected clause - for inheritance (see Ch. 12)
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
ICS 313 - Fundamentals of Programming Languages 6
11.5 Language Examples (continued)
C++ (continued)
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 tilda (~)
See class definition for stack (p. 447) and the example program
that uses it (p. 448)
Friend functions or classes - to provide access to private
members to some unrelated units or functions (NECESSARY in
C++)
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
11.5 Language Examples (continued)
A Related Language: 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
See Java class definition for stacks (p. 449-450) and the
class that uses it (p. 450)
ICS 313 - Fundamentals of Programming Languages 7
11.6 Parameterized Abstract Dats Types
Ada Generic Packages
Make the stack type more flexible by making the element type and the size of the
stack generic
See GENERIC_STACK package (p. 451)
package INT_STACK is new GENERIC_STACK(100, INTEGER);
package FLOAT_STACK is new GENERIC_STACK(500, FLOAT);
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 (100) stk;
The stack element type can be parameterized by making the class a templated
class
See the templated class stack (p. 452-453)
Java does not support generic abstract data types

More Related Content

What's hot

Primitive data types
Primitive data typesPrimitive data types
Primitive data types
bad_zurbic
 
Python Programming - XII. File Processing
Python Programming - XII. File ProcessingPython Programming - XII. File Processing
Python Programming - XII. File Processing
Ranel Padon
 

What's hot (16)

Unit 5
Unit 5Unit 5
Unit 5
 
Question bank unit i
Question bank unit iQuestion bank unit i
Question bank unit i
 
Introduction to database-ER Model
Introduction to database-ER ModelIntroduction to database-ER Model
Introduction to database-ER Model
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
 
Vhdl
VhdlVhdl
Vhdl
 
Object oriented programming using c++
Object oriented programming using c++Object oriented programming using c++
Object oriented programming using c++
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
 
Data abstraction and object orientation
Data abstraction and object orientationData abstraction and object orientation
Data abstraction and object orientation
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Datatype
DatatypeDatatype
Datatype
 
Primitive data types
Primitive data typesPrimitive data types
Primitive data types
 
Introduction java programming
Introduction java programmingIntroduction java programming
Introduction java programming
 
Chapter 04 object oriented programming
Chapter 04 object oriented programmingChapter 04 object oriented programming
Chapter 04 object oriented programming
 
Python Programming - XII. File Processing
Python Programming - XII. File ProcessingPython Programming - XII. File Processing
Python Programming - XII. File Processing
 
Introduction on Data Structures
Introduction on Data StructuresIntroduction on Data Structures
Introduction on Data Structures
 

Viewers also liked

Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Hassan Ahmed
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
Harry Potter
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)Array linear data_structure_2 (1)
Array linear data_structure_2 (1)
eShikshak
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
Tony Nguyen
 
03 data abstraction
03 data abstraction03 data abstraction
03 data abstraction
Opas Kaewtai
 
Unit 2 Principles of Programming Languages
Unit 2 Principles of Programming LanguagesUnit 2 Principles of Programming Languages
Unit 2 Principles of Programming Languages
Vasavi College of Engg
 
iLive 2011 - Pjotr Tsvetkov: How to start an affiliate program?
iLive 2011 - Pjotr Tsvetkov: How to start an affiliate program?iLive 2011 - Pjotr Tsvetkov: How to start an affiliate program?
iLive 2011 - Pjotr Tsvetkov: How to start an affiliate program?
iLive Conference
 

Viewers also liked (20)

Algo>Abstract data type
Algo>Abstract data typeAlgo>Abstract data type
Algo>Abstract data type
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
 
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)Array linear data_structure_2 (1)
Array linear data_structure_2 (1)
 
Preparation Data Structures 03 abstract data_types
Preparation Data Structures 03 abstract data_typesPreparation Data Structures 03 abstract data_types
Preparation Data Structures 03 abstract data_types
 
Data abstraction the walls
Data abstraction the wallsData abstraction the walls
Data abstraction the walls
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Unit i data structure FYCS MUMBAI UNIVERSITY SEM II
Unit i  data structure FYCS MUMBAI UNIVERSITY SEM II Unit i  data structure FYCS MUMBAI UNIVERSITY SEM II
Unit i data structure FYCS MUMBAI UNIVERSITY SEM II
 
03 data abstraction
03 data abstraction03 data abstraction
03 data abstraction
 
Ch7 implementation
Ch7 implementationCh7 implementation
Ch7 implementation
 
Arrays Data Structure
Arrays Data StructureArrays Data Structure
Arrays Data Structure
 
Slide 3 data abstraction & 3 schema
Slide 3 data abstraction & 3 schemaSlide 3 data abstraction & 3 schema
Slide 3 data abstraction & 3 schema
 
Unit 2 Principles of Programming Languages
Unit 2 Principles of Programming LanguagesUnit 2 Principles of Programming Languages
Unit 2 Principles of Programming Languages
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
 
D.SCBL-NEW
D.SCBL-NEWD.SCBL-NEW
D.SCBL-NEW
 
iLive 2011 - Pjotr Tsvetkov: How to start an affiliate program?
iLive 2011 - Pjotr Tsvetkov: How to start an affiliate program?iLive 2011 - Pjotr Tsvetkov: How to start an affiliate program?
iLive 2011 - Pjotr Tsvetkov: How to start an affiliate program?
 
The seen and unseen about metals
The seen and unseen about metalsThe seen and unseen about metals
The seen and unseen about metals
 

Similar to 11 abstract data types

Abstract data types
Abstract data typesAbstract data types
Abstract data types
Fraboni Ec
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
James Wong
 
Object oriented basics
Object oriented basicsObject oriented basics
Object oriented basics
vamshimahi
 
.Net framework
.Net framework.Net framework
.Net framework
Raghu nath
 

Similar to 11 abstract data types (20)

.Net assembly
.Net assembly.Net assembly
.Net assembly
 
Polymorphism and interface in vb.net
Polymorphism and interface in vb.netPolymorphism and interface in vb.net
Polymorphism and interface in vb.net
 
Object Oriented Programming In .Net
Object Oriented Programming In .NetObject Oriented Programming In .Net
Object Oriented Programming In .Net
 
Mca 504 dotnet_unit3
Mca 504 dotnet_unit3Mca 504 dotnet_unit3
Mca 504 dotnet_unit3
 
C# Private assembly
C# Private assemblyC# Private assembly
C# Private assembly
 
Frame class library and namespace
Frame class library and namespaceFrame class library and namespace
Frame class library and namespace
 
C#.net interview questions for dynamics 365 ce crm developers
C#.net interview questions for dynamics 365 ce crm developersC#.net interview questions for dynamics 365 ce crm developers
C#.net interview questions for dynamics 365 ce crm developers
 
Abap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorialsAbap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorials
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Object oriented basics
Object oriented basicsObject oriented basics
Object oriented basics
 
Namespaces in C#
Namespaces in C#Namespaces in C#
Namespaces in C#
 
OOP-Advanced_Programming.pptx
OOP-Advanced_Programming.pptxOOP-Advanced_Programming.pptx
OOP-Advanced_Programming.pptx
 
Data Structure Interview Questions & Answers
Data Structure Interview Questions & AnswersData Structure Interview Questions & Answers
Data Structure Interview Questions & Answers
 
Pcom xpcom
Pcom xpcomPcom xpcom
Pcom xpcom
 
.Net framework
.Net framework.Net framework
.Net framework
 
Assembly
AssemblyAssembly
Assembly
 
Java notes jkuat it
Java notes jkuat itJava notes jkuat it
Java notes jkuat it
 

More from jigeno

Basic introduction to ms access
Basic introduction to ms accessBasic introduction to ms access
Basic introduction to ms access
jigeno
 
16 logical programming
16 logical programming16 logical programming
16 logical programming
jigeno
 
15 functional programming
15 functional programming15 functional programming
15 functional programming
jigeno
 
15 functional programming
15 functional programming15 functional programming
15 functional programming
jigeno
 
14 exception handling
14 exception handling14 exception handling
14 exception handling
jigeno
 
13 concurrency
13 concurrency13 concurrency
13 concurrency
jigeno
 
12 object oriented programming
12 object oriented programming12 object oriented programming
12 object oriented programming
jigeno
 
9 subprograms
9 subprograms9 subprograms
9 subprograms
jigeno
 
8 statement-level control structure
8 statement-level control structure8 statement-level control structure
8 statement-level control structure
jigeno
 
7 expressions and assignment statements
7 expressions and assignment statements7 expressions and assignment statements
7 expressions and assignment statements
jigeno
 
6 data types
6 data types6 data types
6 data types
jigeno
 
5 names
5 names5 names
5 names
jigeno
 
4 lexical and syntax analysis
4 lexical and syntax analysis4 lexical and syntax analysis
4 lexical and syntax analysis
jigeno
 
3 describing syntax and semantics
3 describing syntax and semantics3 describing syntax and semantics
3 describing syntax and semantics
jigeno
 
2 evolution of the major programming languages
2 evolution of the major programming languages2 evolution of the major programming languages
2 evolution of the major programming languages
jigeno
 
1 preliminaries
1 preliminaries1 preliminaries
1 preliminaries
jigeno
 
Access2007 m2
Access2007 m2Access2007 m2
Access2007 m2
jigeno
 
Access2007 m1
Access2007 m1Access2007 m1
Access2007 m1
jigeno
 

More from jigeno (20)

Access2007 part1
Access2007 part1Access2007 part1
Access2007 part1
 
Basic introduction to ms access
Basic introduction to ms accessBasic introduction to ms access
Basic introduction to ms access
 
Bsit1
Bsit1Bsit1
Bsit1
 
16 logical programming
16 logical programming16 logical programming
16 logical programming
 
15 functional programming
15 functional programming15 functional programming
15 functional programming
 
15 functional programming
15 functional programming15 functional programming
15 functional programming
 
14 exception handling
14 exception handling14 exception handling
14 exception handling
 
13 concurrency
13 concurrency13 concurrency
13 concurrency
 
12 object oriented programming
12 object oriented programming12 object oriented programming
12 object oriented programming
 
9 subprograms
9 subprograms9 subprograms
9 subprograms
 
8 statement-level control structure
8 statement-level control structure8 statement-level control structure
8 statement-level control structure
 
7 expressions and assignment statements
7 expressions and assignment statements7 expressions and assignment statements
7 expressions and assignment statements
 
6 data types
6 data types6 data types
6 data types
 
5 names
5 names5 names
5 names
 
4 lexical and syntax analysis
4 lexical and syntax analysis4 lexical and syntax analysis
4 lexical and syntax analysis
 
3 describing syntax and semantics
3 describing syntax and semantics3 describing syntax and semantics
3 describing syntax and semantics
 
2 evolution of the major programming languages
2 evolution of the major programming languages2 evolution of the major programming languages
2 evolution of the major programming languages
 
1 preliminaries
1 preliminaries1 preliminaries
1 preliminaries
 
Access2007 m2
Access2007 m2Access2007 m2
Access2007 m2
 
Access2007 m1
Access2007 m1Access2007 m1
Access2007 m1
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Recently uploaded (20)

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 

11 abstract data types

  • 1. ICS 313 - Fundamentals of Programming Languages 1 11. Abstract Data Types 11.1 The Concept of Abstraction The concept of abstraction is fundamental in programming 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
  • 2. ICS 313 - Fundamentals of Programming Languages 2 11.2 Encapsulation Original motivation: Large programs have two special needs: Some means of organization, other than simply division into subprograms 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 Examples of Encapsulation Mechanisms Nested subprograms in some ALGOL-like languages (e.g., Pascal) FORTRAN 77 and C - Files containing one or more subprograms can be independently compiled FORTRAN 90 and Ada - separately compilable modules 11.3 Introduction to Data Abstraction 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.
  • 3. ICS 313 - Fundamentals of Programming Languages 3 11.3 Introduction to Data Abstraction (continued) Advantage of Restriction 1: Same as those for encapsulation: 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 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 11.4 Design Issues Language Requirements to support abstract data types: A syntactic unit in which to encapsulate the type definition. A method of making type names and subprogram headers visible to clients, while hiding actual definitions. 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 Language Design Issues: Encapsulate a single type, or something more? What types can be abstract? Can abstract types be parameterized? What access controls are provided?
  • 4. ICS 313 - Fundamentals of Programming Languages 4 11.5 Language Examples Simula 67 Provided encapsulation, but no information hiding Ada The encapsulation construct is the package Packages usually have two parts: Specification package (the interface) Body package (implementation of the entities named in the specification Information Hiding Hidden types are named in the spec package, as in: type NODE_TYPE is private; 11.5 Language Examples (continued) 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; … A spec package can also define unhidden types simply by providing the representation outside a private clause The reasons for the two-part type definition are: The compiler must be able to see the representation after seeing only the spec package (the compiler can see the private clause) Clients must see the type name, but not the representation (clients cannot see the private clause)
  • 5. ICS 313 - Fundamentals of Programming Languages 5 11.5 Language Examples (continued) Private types have built-in operations for assignment and comparison with = and /= Limited private types have no built-in operations ---> SHOW specification and body packages (pp. 443-444) and the procedure that uses them (p. 445) 11.5 Language Examples (continued) 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 Information Hiding: Private clause for hidden entities Public clause for interface entities Protected clause - for inheritance (see Ch. 12) 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
  • 6. ICS 313 - Fundamentals of Programming Languages 6 11.5 Language Examples (continued) C++ (continued) 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 tilda (~) See class definition for stack (p. 447) and the example program that uses it (p. 448) Friend functions or classes - to provide access to private members to some unrelated units or functions (NECESSARY in C++) 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 11.5 Language Examples (continued) A Related Language: 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 See Java class definition for stacks (p. 449-450) and the class that uses it (p. 450)
  • 7. ICS 313 - Fundamentals of Programming Languages 7 11.6 Parameterized Abstract Dats Types Ada Generic Packages Make the stack type more flexible by making the element type and the size of the stack generic See GENERIC_STACK package (p. 451) package INT_STACK is new GENERIC_STACK(100, INTEGER); package FLOAT_STACK is new GENERIC_STACK(500, FLOAT); 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 (100) stk; The stack element type can be parameterized by making the class a templated class See the templated class stack (p. 452-453) Java does not support generic abstract data types