Title: Abstraction I : Encapsulation
1Abstraction I Encapsulation
2Text References
3Content
- Abstract Data Types
- Encapsulation by Subprograms
- Type Definitions
- Storage Management
4Creation of New Data Types and Operations
- Subprograms
- Creating subprograms that perform the
functionality of the new type - Correct use of the new type is the responsibility
of the programmer with little support provided by
the programming language - Type declarations
- Defining new types and operations on that type
- Providing some support for detecting invalid uses
of the new type - Inheritance
5Abstract Data Types
- Early languages (FORTRAN, COBOL) limit the
creation of new data types to subprogram
definitions. - As the concept of data type has evolved, new
language designs (ADA, C) have provided better
facilities for specifying and implementing entire
abstract data types.
6Evolution of the Data Type Concept
- Actual computer include no provision for defining
and enforcing data type restrictions. - Higher-level languages provide a set of data
type, and type checking is provided to ensure
that operations are not applied to data of the
wrong type.
a set of data objects the set of operations
a set of values
a general type definition
7Evolution of the Data Type Concept
- Data abstraction
- A set of data objects, ordinarily using one or
more type definitions. - A set of abstract operations on those data
objects, and - Encapsulation of the whole in such a way that the
user of the new type cannot manipulate data
objects of the type except by the use of the
operation defined. - The user needs to know only the type name and the
semantics of the available operations.
8Information Hiding
- A large program exceeds the intellectual grasp of
a single person. - Dividing into modules, performing a limited set
of operation on a limited amount of data, keeps
intellectual control over the program design
process. - Module design follows two approaches
- Modules represent functional decomposition of the
program, or - Modules represent a data decomposition of the
program.
9Information Hiding
section
Functional view must know the details of section
Simple abstraction only need to know the
specification the implementation details are
hidden and can be ignored
registration
maintenance
10Information Hiding
- Abstraction is so pervasive in programming
activities - The layering of hardware and software
- A flowchart is an abstraction of the
statement-level control structure of a program - Methods of designing programs stepwise
refinement, structured programming, modular
programming, and top-down programming
11Information Hiding
- A programming language provides support for
abstraction in two ways - By providing a virtual computer that is simpler
to user and more powerful than the actual
underlying computer - Providing facilities that aid the programmer to
construct abstractions (subprograms, subprogram
libraries, type definitions, classes, and
packages).
12Information Hiding
- Information hiding is the central principle in
the design of programmer-defined abstraction - Each such program component should hide as much
information as possible. - A square-root function is successful if it hides
the details of the representation and
computation. - When information is encapsulated in an
abstraction, it means that the user of the
abstraction - Does not need to know the hidden information in
order to use the abstraction and - Is not permitted to directly use or manipulate
the hidden information even if desiring to do so.
13Information Hiding
- Subprograms form a basic encapsulation mechanism
that is presented in almost every language. - Mechanisms that allow encapsulation of entire
data type definitions are more recent, and appear
only in Ada and C (and Java)
14Encapsulation by Subprograms
- An abstract operation defined by the programmer
- Two views
- Program design level the sense in which a
subprogram represents an abstraction - Language design level concerning with the design
and implementation of the general facilities for
subprogram definition and invocation.
15Subprograms as Abstract Operations
- As with primitive operations, a subprogram
definition has two parts - a specification, and
- an implementation
16Specification of a Subprogram
- Including
- The name of the subprogram
- The signature of the subprogram giving
- the number of arguments, their order, and the
data type of each, and - the number of results, their order, and the data
type of each - The action performed by the subprogram
17Specification of a Subprogram
- If a subprogram returns a single-result data
object explicitly, it is termed a functional
subprogram. - If a subprogram returns more than one result, or
it modifies its arguments rather than returning
result explicitly, it is termed a procedure or
subroutine.
function FX(Xreal Yinteger) real Pascal
void sub(float X, int Y, float Z, int W)
C procedure sub(Xin real Yin integer
Z in out Real Wout Boolean) Ada
18Specification of a Subprogram
- Problems in attempting to describe precisely the
function computed - Implicit arguments
- Implicit results
- Incomplete execution for given arguments
(exception) - History sensitive
19Implementation of a Subprogram
- Implemented using the data structures and
operations provided by the programming language
itself - Body local data declaration statements
- Each invocation requires arguments of the proper
types, the type of result must also be known - Type checking issues are similar to those for
primitive operations.
20Subprogram Definition and Invocation
- Subprogram
- Definition (a template for creating activation)
- Activation (created if being invoked, destroyed
if execution is complete) - Definition vs. activation
- Definition is the only information available
during translation. - Activation exists during execution.
21Implementation of Subprogram Definition and
Invocation
float FN(float X, int Y) const initval2
define finalval 10 float M(10) int N
Ninitval if(Nltfinalval) return
(20XM(N))
- FNs signature provides information for storage
for parameter and storage for the function
result. - Declarations provide for storage for local
variables. - Storage for literals and defined constants.
- Storage for executable code
22Implementation of Subprogram Definition and
Invocation
23Implementation of Subprogram Definition and
Invocation
24Generic Subprograms
- A generic subprogram is one with a single name
but several different definitions, distinguished
by a different signature. - A generic subprogram name is said to be
overloaded.
procedure Enter(Student in integer Sec in out
Section) is begin - statements to enter student
in a section roll list end
procedure Enter(S in Section Tab in out
Classlist) is begin - statements to enter
section in a classlist end
25Subprogram Definitions as Data objects
- In most compiled languages, subprogram definition
is independent from subprogram execution.
(compile-time, run-time) - In languages such as LISP and Prolog, there is no
distinction between these phases. - In both languages, it is possible to begin
program execution without having a particular
subprogram in existence. - During execution, the subprogram body may be read
in or created as a character data object and then
translated into executable form.
26Type Definitions
- In defining a complete new abstract data type,
some mechanism is required for definition of a
class of data objects. - Type definition
- A type definition
- a type name together with
- a declaration that describes the structure of a
class of data objects
type Rational record numerator integer
denominator integer end var A, B, C Rational
Pascal
27Type Definitions
struct RationalType int numerator int
denominator struct RationalTyle A, B, C
typedef struct RationalType int numerator
int denominator Rational Rational A, B, C
28Type Equivalence
- Type checking
- Comparing whether two types are the same or
different - Type equivalence brings up two related concepts
- What does it means to say that two types are the
same? and (data type issue) - What does it mean to say that two data objects of
the same type are equal? (semantic issue)
29Type Equality
- When is type X the same as type Y?
program main(input,output) type Vect1
array1..10 of real Vect2 array1..10
of real var X,Z Vect1 Y Vect 2 procedure
Sub(AVect1) end begin XY
Sub(Y) end.
- Two general solutions to this problem
- Name equivalence
- Structural equivalence
30Name Equivalence
- Two data types are considered equivalent only if
they have the same name.
program main(input,output) type Vect1
array1..10 of real Vect2 array1..10
of real var X,Z Vect1 Y Vect 2 procedure
Sub(AVect1) end begin XY XZ
Sub(Y) end.
different
invalid
valid
31Name Equivalence
- Disadvantages
- Every object used in an assignment must have a
type name - var W array1..10 of real
- W has a distinct type, but it cannot be used as
an argument to a subprogram, since its type has
no name. - A single type definition must serve all or large
parts of a program.
32Structural Equivalence
- Two data types are considered equivalent if they
define data objects that have the sane internal
component. - Vect1 and Vect2 are equivalent types
- Disadvantages
- Subtle questions arise as to when two types are
structurally equivalent - Component names of record must be identical?
- Does it suffice to have the same number and type
of components in the same order?
33Structural Equivalence
- Disadvantages
- Two variables may inadvertently be structurally
equivalent, even though the programmer declares
them as separate types. - type Metersinteger
- Litersinteger
- var LenMeters
- Vol Liters
- Cost
34Storage Management
- Storage for data is one of the central concerns
of the programmers, language implementors, and
language designers.
35Major Run-Time Elements Requiring Storage
- Code segment for translated user programs
- User-defined data structures and constants
- Subprogram return points
- Referencing environment
- Temporaries in expression evaluation
- Temporaries in parameter transmission
- Input-Output buffers
- Miscellaneous system data
- Subprogram call and return operations
- Data structure creation and destruction
operations - Component insertion and deletion operations
36Programmer- and System-Controlled Storage
Management
- To what extent should the programmer be allowed
to directly control storage management? - In C language malloc, free
- No direct control
- The difficulty with programmer control of storage
management - Burden on the programmer
- Interfering the system-controlled storage
management
37Storage Management Phases
- Initial allocation
- Techniques for keeping track of free storage as
well as mechanism for allocation of free storage - Recovery
- Managing for reuse
- Compaction and reuse
- To construct large block of free storage
38Static Storage Management
- The simplest form of allocation is static
allocation, allocation during translation that
remains fixed throughout execution. - Efficient because no time or space is expended
for storage management during execution. - Incompatible with recursive subprogram call
39Stack-Based Storage Management
- The simplest run-time storage management
technique. - Free storage at the start of execution is set up
as a sequential block in memory. - As storage is allocated, it is taken from
sequential locations in this stack block
beginning at one end. - Storage must be freed in the reverse order of
allocation so that a block of storage being freed
is always at the top of the stack.
40(No Transcript)
41Heap Storage Management Fixed-Size Elements
- A heap is a block of storage within which pieces
are allocated and freed in some relatively
unstructured manner. - Here the problems of storage allocation,
recovery, compaction, and reuse may be severe. - Fig. 5.7, p. 225
42Heap Storage Management Variable-Size Elements
- Variable-size elements arise in many situations
- Array used for programmer-defined data structures
stored sequentially - Difficulties
- Reuse of recovered space