ADTOOP
ADTOOP
1)
COEN 171 - Data Abstraction and OOP
Data Abstraction
– Problems with subprogram abstraction
– Encapsulation
– Data abstraction
– Language issues for ADTs
– Examples
» Ada
» C++
» Java
– Parameterized ADTs
(11.2)
COEN 171 - Data Abstraction and OOP
Object-oriented programming
– Components of object-oriented programming languages
– Fundamental properties of the object-oriented model
– Relation to data abstraction
– Design issues for OOPL
– Examples
» Smalltalk 80
» C++
» Ada 95
» Java
– Comparisons
» C++ and Smalltalk
» C++ and Ada 95
» C++ and Java
– Implementation issues
(11.3)
Subprogram Problems
No way to selectively provide visibility for subprograms
No convenient ways to collect subprograms together to perform a
set of services
Program that uses subprogram (client program) must know
details of all data structures used by subprogram
– client can “work around” services provided by subprogram
– hard to make client independent of implementation techniques for
data structures
» discourages reuse
Difficult to build on and modify the services provided by
subprogram
Many languages don’t provide for separately compiled
subprograms
(11.4)
Encapsulation
One solution
– a grouping of subprograms that are logically related that can be
separately compiled
– called encapsulations
Examples of encapsulation mechanisms
– nested subprograms in some ALGOL-like languages
» Pascal
– FORTRAN 77 and C
» files containing one or more subprograms can be independently
compiled
– FORTRAN 90, Modula-2, Modula-3, C++, Ada (and other
contemporary languages)
» separately compilable modules
(11.5)
Data Abstraction
A better solution than just encapsulation
Can write programs that depend on abstract properties of a type,
rather than implementation
Informally, an Abstract Data Type (ADT) is a [collection of] data
structures and operations on those data structures
– example is floating point number
» can define variables of that type
» operations are predefined
» representation is hidden and can’t manipulate except through built-in
operations
ADT
– isolates programs from the representation
– maintains integrity of data structure by preventing direct
manipulation
(11.6)
Data Abstraction (continued)
Formally, an ADT is a user-defined data type where
– 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.
– 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.
Advantages of first restriction are same as those for encapsulation
– program organization
– modifiability (everything associated with a data structure is
together)
– separate compilation
(11.7)
Data Abstraction (continued)
Advantage of second restriction is 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
By this definition, built-in types are ADTs
– 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
(11.8)
Data Abstraction (continued)
ADTs provide mechanisms to limit visibility
– public part indicates what can be seen (and used from) outside
» what is exported
– private part describes what will be hidden from clients
» made available to allow compiler to determine needed information
» C++ allows specified program units access to the private information
• friend functions and classes
(11.9)
Language Issues for ADTs
Language requirements for data abstraction
– 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
» public/private
– 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
Can put ADTs in PL
– as a type definition extended to include operations (C++)
» use directly to declare variables
– as a collection of objects and operations (Ada)
» may need to be instantiated before declaring variables
(11.10)
Language Issues for ADTs (continued)
Language design issues
– encapsulate a single type, or something more?
– what types can be abstract?
– can abstract types be parameterized?
– how are imported types and operations qualified?
Simula-67 was first language to address this issue
– classes provided encapsulation, but no information hiding
(11.11)
Data Abstraction in Ada
Abstraction mechanism is the package
Each package has two pieces (can be in same or separate files)
– specification
» public part
» private part
– body
» implementation of all operations exported in public part
» may include other procedures, functions, type and variable declarations, which
are hidden from clients
• all variables are static
» may provide initialization section
• executed when declaration involving package is elaborated
(continued)
Polymorphism
– special kind of dynamic binding
» message to method
– same message can be sent to different objects, and the object will
respond properly
– similar to function overloading except
» overloading is static (known at compile time)
» polymorphism is dynamic (class of object known at run time)
(11.24)
Comparison with Data Abstraction
Class == generic package
Object == instantiation of generic
– actually, closer to instance of exported type
Messages == calls to operations exported by ADT
Methods == bodies (code) for operations exported by ADT
EXCEPT
– data abstraction mechanism allows only one level of
generic/instantiation
– OO model allows multiple levels of inheritance
– no dynamic binding of method invocation in ADTs
(11.25)
OOP Language Design Issues
Exclusivity of objects
– everything is an object
» elegant and pure, but slow for primitive types
– add objects to complete typing system
» fast for primitive types, but confusing
– include an imperative style typing system for primitive types, but
everything else is an object
» relatively fast, and less confusion
Are subclasses subtypes?
– does an “is a” relationship hold between parent and child classes?
(11.26)
OOP Language Design Issues (continued)
Interface or implementation inheritance?
– if only interface of parent class is visible to subclass, interface
inheritance
» may be inefficient
– if interface and implementation visible to subclass, implementation
inheritance
Type checking and polymorphism
– if overridding methods must have the same parameter types and
return type, checking may be static
– Otherwise need dynamic type checking, which is slow and delays
error detection
Single or multiple inheritance
– multiple is extremely convenient
– multiple also makes the language and implementation more
complex, and is less efficient
(11.27)
OOP Language Design Issues (continued)
Allocation and deallocation of objects
– if all objects are allocated from heap, references to them are uniform
(as in Java)
– is deallocation explicit (heap-dynamic objects in C++) or implicit
(Java)
Should all binding of messages to methods be dynamic?
– if yes, inefficient
– if none are, great loss of flexibility
(11.28)
Smalltalk 80
Smalltalk is the prototypical pure OOPL
All entities in a program are objects
– referenced by pointers
All computation is done by sending messages (perhaps
parameterized by object names) to objects
– message invokes a method
– reply returns result to sender, or notifies that action has been done
Also incorporates graphical programming environment
– program editor
– compiler
– class library browser
» with associated classes
– also written in Smalltalk
» can be modified
(11.29)
Smalltalk 80 (continued)
Messages
– object to receive message
– message
» method to invoke
» possibly parameters
Unary messages
– specify only object and method
– firstAngle sin
» invokes sin method of firstAngle object
Binary messages
– infix order
– total / 100
» sends message / 100 to object total
» which invokes / method of total with parameter 100
(11.30)
Smalltalk 80 (continued)
Keyword messages
– indicate parameter values by specifying keywords
– keywords also identify the method
– firstArray at: 1 put: 5
» invokes at:put: method of firstArray with parameters 1 and 5
Message expressions
– messages may be combined in expressions
» unary have highest precedence, then binary, then keyword
» associate left to right
» order may be specified by parentheses
– messages may be cascaded
» ourPen home; up; goto: 500@500
» equivalent to ourPen home.
ourPen up.
ourPen goto: 500@500
(11.31)
Smalltalk 80 (continued)
Assignment
– object <- object
– index <- index + 5
Blocks
– unnamed objects specified by [ <expressions> ]
» expressions are separated by .
– evaluated when they are sent the value message
» always in the context of their definition
– may be assigned to variables
» foo <- [ ... ]
Logical loops
– blocks may contain conditions [ <logical condition> ]
– all blocks have whileTrue methods whileTrue:
– sends value to condition block
[ <body of loop> ]
– evaluates body block if result is true
(11.32)
Smalltalk 80 (continued)
Iterative loops
– all integer objects have a timesRepeat method
– also have 12 timesRepeat: [ ... ]
» to:do:
» to:by:do:
6 to: 10 do: [ ... ]
– a block is the loop body
Selection
– true and false are also objects
– each has ifTrue:, ifFalse:, ifTrue:ifFalse:, and IfFalse:ifTrue:
methods
total = 0 “returns true or false object”
ifTrue: [ ... ] “true object executes this; false ignores”
ifFalse: [ ... ] “false object executes this; true ignores”
(11.33)
Smalltalk 80 (continued)
Dynamic binding
– when a message arrives at an object, the class of which the object is
an instance is searched for a corresponding method
– if not there, search superclass, etc.
Only single inheritance
– every class is an offspring of the root class Object
Evaluation
– simple, consistent syntax
– relatively slow
» message passing overhead for all control constructs
» dynamic binding of message to method
– dynamic binding allows type errors to be detected only at run-time
(11.34)
C++
Essentially all of variable declaration, types, and control
structures are those of C
C++ classes represent an addition to type structure of C
Inheritance
– multiple inheritance allowed
– classes may be stand-alone
– three information hiding modes
» public: everyone may access
» private: no one else may access
» protected: class and subclasses may access
– when deriving a class from a base class, specify a protection mode
» public mode: public, protected, and private are retained in subclass
» private mode: everything in base class is private
• may reexport public members of base class
(11.35)
C++ (continued)
Dynamic binding
– C++ member functions are statically bound unless the function
definition is identified as virtual
– if virtual function name is called with a pointer or reference variable
with the base class type, which member function to execute must be
determined at run-time
– pure virtual functions are set to 0 in class header
» must be redefined in derived classes
– classes containing a pure virtual function can never be instantiated
directly
» must be derived
(11.36)
Java
General characteristics
– all data are objects except the primitive types
– all primitive types have wrapper classes that store one data value
– all objects are heap-dynamic, referenced through reference variables, and
most are explicitly allocated
Inheritance
– single inheritance only
» but implementing interface can provide some of the benefits of multiple
inheritance
» an interface can include only method declarations and named constants
Package Object_Package is
type Object is tagged private;
procedure Draw (O: in out Object);
private
type Object is tagged record
X_Coord, Y_Coord: Real;
end record;
end Object_Package;
(11.39)
Ada 95 (continued)
Then may derive a new class by using new reserved word and
modifying tagged type exported
Overloading defines new methods