Lecture 7: Abstract Data Types and Encapsulation
Constructs
Marriette Katarahweire and John Kizito
CSC 3112: Principles of Programming Languages 1/26
Outline
Chapter 11, Concepts of Programming Languages by Sebesta
General concept of abstraction
Definition and illustration of data abstraction
Describe support for data abstraction in some PLs
Parameterized abstract data types
CSC 3112: Principles of Programming Languages 2/26
Similar Concepts
Abstraction: Omitting or hiding low-level details with a
simpler, higher-level idea.
Modularity: Dividing a system into components or modules,
each of which can be designed, implemented, tested, reasoned
about, and reused separately from the rest of the system.
Encapsulation: Building walls around a module (a hard shell
or capsule) so that the module is responsible for its own
internal behavior, and bugs in other parts of the system can’t
damage its integrity.
Information hiding: Hiding details of a module’s
implementation from the rest of the system, so that those
details can be changed later without changing the rest of the
system.
Separation of concerns: Making a feature (or “concern”)
the responsibility of a single module, rather than spreading it
across multiple modules.
CSC 3112: Principles of Programming Languages 3/26
The Concept of Abstraction
An abstraction is a view or representation of an entity that
includes only the most significant attributes
The concept of abstraction is fundamental in programming
(and computer science)
2 kinds of abstraction: process abstraction and data
abstraction
Nearly all programming languages support process abstraction
with subprograms
Nearly all programming languages designed since 1980
support data abstraction
Purpose of abstraction is to simplify the programming process
- allow the programmer to focus on the essential attributes
while ignoring the subordinate attributes.
Abstraction helps manage complexity: a means in which
large/complicated programs are made more manageable.
Is a fundamental component of Object-oriented programming
CSC 3112: Principles of Programming Languages 4/26
The Concept of Abstraction: Encapsulation
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
- These are called encapsulations
CSC 3112: Principles of Programming Languages 5/26
Abstract Data Types
ADT satisfies the following two conditions:
The representation of, and operations on, objects of the type
are defined in a single syntactic unit
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
CSC 3112: Principles of Programming Languages 6/26
Abstract Data Types
Advantage of the first condition
- Program organization
- enhances modifiability (everything associated with the data
structure is in one place)
- separate compilation, debugging
Advantage of the second condition (Information hiding)
- Reliability: by hiding the data representations, user code
cannot directly access objects of the type or depend on the
representation, allowing the representation to be changed
without affecting user code
- flexibility
- reduced name conflicts
CSC 3112: Principles of Programming Languages 7/26
Abstract Data Types
Built-in types are abstract data types e.g. int type in C
- 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
CSC 3112: Principles of Programming Languages 8/26
ADT Language Requirements
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
CSC 3112: Principles of Programming Languages 9/26
Data Abstraction Example
sortInt(list, listLength)
essential attributes: name of array to be sorted, type of its
elements, array’s length, fact that the call to sortInt will result
in the array being sorted
subordinate attributes: the particular algorithm that sortInt
implements
CSC 3112: Principles of Programming Languages 10/26
Abstract Data Type
An ADT is an enclosure that includes only the data
representation of one specific data type and the subprograms
that provide the operations for that type
Through access controls, unnecessary details of the type can
be hidden from units outside the enclosure that use the type.
Program units that use an abstract data type can declare
variables of that type, even though the actual representation
is hidden from them.
An instance of an abstract data type is called an object
CSC 3112: Principles of Programming Languages 11/26
User-defined ADTs
should provide the same characteristics as those of
language-defined types, such as a floating-point type
a type definition that allows program units to declare variables
of the type but hides the representation of objects of the type
a set of operations for manipulating objects of the type
CSC 3112: Principles of Programming Languages 12/26
User-defined ADT
An abstract data type is a data type that satisfies the following
conditions:
The representation of objects of the type is hidden from the
program units that use the type, so the only direct operations
possible on those objects are those provided in the type’s
definition.
The declarations of the type and the protocols of the
operations on objects of the type, which provide the type’s
interface, are contained in a single syntactic unit. The type’s
interface does not depend on the representation of the objects
or the implementation of the operations. Also, other program
units are allowed to create variables of the defined type.
CSC 3112: Principles of Programming Languages 13/26
Accessor Methods
Program units that use a specific abstract data type are called
clients of that type
situations arise in which clients need to access the data
members. The common solution is to provide accessor
methods; getters and setters
allow clients indirect access to the so called hidden data; a
better solution than simply making the data public, which
would provide direct access
CSC 3112: Principles of Programming Languages 14/26
Accessor Methods
three reasons why accessors are a better solution than simply
making the data public:
Read-only access can be provided, by having a getter method
but no corresponding setter method.
Constraints can be included in setters. For example, if the
data value should be restricted to a particular range, the
setter can enforce that.
The actual implementation of the data member can be
changed without affecting the clients if getters and setters are
the only access.
Both specifying data in an abstract data type to be public and
providing accessor methods for that data are violations of the
principles of abstract data types
CSC 3112: Principles of Programming Languages 15/26
Accessor Methods Example - Java
public class Cat {
private int Age;
public int getAge() {
return this.Age;
}
public void setAge(int Age) {
this.Age = Age;
}
}
which is the setter, getter, accessor, mutator method?
CSC 3112: Principles of Programming Languages 16/26
C++ Example
Encapsulation is via classes
ADT based on C struct, Simula 67 class
Classes are types
All instances of a class share copy of member functions
(methods)
Each instance has its own copy of class data members
(instance variables)
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 (later)
CSC 3112: Principles of Programming Languages 17/26
C++ Example
Constructors:
• Functions to initialize the data members — they don’t
create 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 — but can be
called explicitly
• Name is the same as the class name
Destructors:
• Clean up after an instance is destroyed — usually just to
reclaim heap storage
• Implicitly called when the object’s lifetime ends, or explicitly
called
• Name is the class name, preceded by a tilde ( )
CSC 3112: Principles of Programming Languages 18/26
Parameterized ADTs
allow designing an ADT that can store any type elements
Also known as generic classes
C++ and Ada provide support for parameterized ADTs
CSC 3112: Principles of Programming Languages 19/26
Parameterized ADTs in C++
Classes can be somewhat generic by writing parameterized
constructor functions
class Stack {
...
Stack (int size) {
stk_ptr = new int [size];
max_len = size - 1;
top = -1;
};
...
}
Stack stk(100);
CSC 3112: Principles of Programming Languages 20/26
Encapsulation constructs for large programs
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)
Such collections are called encapsulation
CSC 3112: Principles of Programming Languages 21/26
Nested Subprograms
Organizing programs by nesting subprogram definitions inside
the logically larger subprograms that use them
Nested subprograms are supported in Ada, Fortran 95,
Python, and Ruby
CSC 3112: Principles of Programming Languages 22/26
Encapsulation in C
Files containing one or more subprograms can be
independently compiled
The interface to such file (library) is placed in a header file
The header file is used in the client code, using the #include
preprocessor specification, so that references to functions and
data in the client code can be type-checked
CSC 3112: Principles of Programming Languages 23/26
Naming Encapsulations
Large programs may consist of many independently written
codes, where naming conflicts might occur.
A naming encapsulation define name scopes that assist in
avoiding name conflicts.
C++ Namespaces: Can place each library in its own
namespace and qualify names used outside with the
namespace
Example of declaration:
namespace MyStack
Example of references:
using MyStack :: topPtr ;
using namespace MyStack;
CSC 3112: Principles of Programming Languages 24/26
Java Packages
Packages can contain more than one class definition
Clients of a package can use fully qualified name or use the
import declaration
Example of declaration: package myStack;
Example of references: import myStack.∗
import myStack.topPtr ;
CSC 3112: Principles of Programming Languages 25/26
Exercise
Try out Review Questions 1 - 5, 30, 34, 41, 43,44
Problem Set 1, 3, 5, 12, 15, 16
Programming Exercises 1, 5 - 10
CSC 3112: Principles of Programming Languages 26/26