You are on page 1of 20

Lecture 7: Abstract Data Types and

Encapsulation

Marriette Katarahweire

CSC 3112: Principles of Programming Languages 1/20


Outline

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/20


The Concept of Data 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 3/20
Data Abstraction

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 4/20


Introduction to Data Abstraction

An abstract data type is a user-defined data type that 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 5/20


Advantages of Data Abstraction

Advantage of the first condition


Program organization, modifiability (everything associated
with a data structure is together), and separate compilation
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

CSC 3112: Principles of Programming Languages 6/20


Question

differentiate between abstraction, information hiding and


encapsulation.

CSC 3112: Principles of Programming Languages 7/20


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 8/20


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 9/20


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 10/20


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 11/20


Accessor Methods

three reasons why accessors are better:


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 12/20


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 13/20


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 14/20


Overloaded Subprograms

An overloaded subprogram is one that has the same name as


another subprogram in the same referencing environment
Every version of an overloaded subprogram has a unique
protocol
C++, Java, C#, and Ada
include predefined overloaded subprograms
allow users to write multiple versions of subprograms with the
same name
usually used to define overloading constructors

CSC 3112: Principles of Programming Languages 15/20


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 16/20


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 17/20


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 18/20


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 19/20


Exercise

Try out Review Questions 1 - 5, 30, 34, 41, 43,44, Problem Set 1,
3, 12, 15, 16, Programming Exercises 1, 5, 10

CSC 3112: Principles of Programming Languages 20/20

You might also like