Docsity

Abstraction and Information Hiding in Object-Oriented Programming, Papers of Programming Languages

Partial preview of the text

Download Abstraction and Information Hiding in Object-Oriented Programming and more Papers Programming Languages in PDF only on Docsity!

Modularity and Object-

oriented Abstractions

Encapsulation, dynamic binding,

subtyping and inheritance

Topics

 Modular program development

 Interface, specification, and implementation

 Language support for abstractions

 Procedural abstraction  Abstract data types  Packages and modules  Generic abstractions  Functions and modules with type parameters

 Object-oriented abstractions

 An abstraction is a data type  Has one or multiple constructors  Can be instantiated to produce objects (values)  Can have subtype relations with other OO abstractions  Can adapt behaviors (implementations) of super classes

Basic Concept: Abstraction

 An abstraction separates interface from implementation  Hide implementation details from outside (the client)  Support program evolution and code reuse  Function/procedure abstraction  Client: caller of the function  Implementation: function body  Interface and specification: function declaration  Enforced by scoping rules  Data abstraction  Client: Algorithms that use the data structure  Implementation: representation of data  Priority queue can be binary search tree or partially-sorted array  Interface and specification: interface of operations on the data structure  Enforced by type system

Example: A Function Abstraction  Hide implementation details of a function  Interface: float sqrt (float x)  Specification: if x>1, then sqrt(x)sqrt(x) ≈ x.  Implementation details float sqrt (float x){ float y = x/2; float step=x/4; int i; for (i=0; i<20; i++){ if ((yy)<x) y=y+step; else y=y-step; step = step/2; } return y; }

ML abstype vs. C++/Java classes

 ML abstype:

 Only data are members of abstraction

 Nameless data accessed through pattern matching

 Access functions are global functions

 Function names are bound in enclosing scope  C++/Java class:

 Both data and functions are members of abstraction

 Every member must have a name

 Access functions are local functions (methods)

 Method names are bound within the class (the class is a scope)

Modules: Combination Of Data And Function Abstractions

 General Support For Information Hiding

 Hide implementation of related data and functions  Interface: a set of names and their types  Implementation  Implementation for every entry in the interface  Additional declarations that are hidden

 Can define multiple data abstractions

 Can also define multiple function abstractions

 Modules in different languages

 ML: signatures, structures and functors (will skip)  C++ namespaces  C++ templates (generic abstractions) (will skip)  Object-oriented abstractions  Java interfaces and classes; C++ classes

Example: Global vs. Local names  Java class: class vehicle { protected: double speed =0, fuel = 0; public void start(double x) {speed = x;} public void refuel (double x) { fuel = fuel + x; } }; vehicle a = new vehicle; a.start(5);  ML abstype abstype vehicle = V of real ref * real ref with fun mk_vehicle() = V(ref 0.0, ref 0.0); fun vehicle_start (V(speed,fuel), x) = speed := x; fun vehicle_refuel (V(speed,fuel), x) = fuel := !fuel + x end; val a = mk_vehicle(); vehicle_start(a,5.0);

Example: C++ namespaces

namespace Stack { // interface in a header file class Underflow { }; // used as exception class Overflow { }; // used as exception void push(char c); // interface for pushing char pop(); // interface for popping }; namespace Stack { // implementation in a cpp file const int max_size = 200; char v[max_size]; int top = 0; void push(char c) { ... } char pop() { ... } };

More C++ Templates(Skip)

 Instantiation at link time  Separate copy of template generated for each type  Why code duplication?  Size of local variables in activation record  Link to operations on parameter type  Standard template library (STL)  Polymorphic (parameterized, generic) abstractions  Data abstractions, function abstractions, modules  Efficient running time (but not always space)  Does not rely on objects – No virtual functions

14

Summary of Abstractions

 Abstractions  Information hiding: interface and implementation details  Function and data abstractions

 Modules: grouping of related data and functions

 Types, variables, constants, functions  Interface: declarations visible to the outside

 Abstractions in different languages

 What are fundamentally different between  ML abstype: data abstraction (hide data representation); all access functions are in the global scope.  C++ namespaces: a group of related data and functions; No explicit access control (separation through file inclusion); not a data type (cannot build values of name spaces)  C++/Java classes: data abstraction + module  What about Java interfaces? (no implementation)

Object-Orientation

 Programming methodology

 Organize concepts into objects and classes  Build extensible systems

 Language concepts

 Encapsulation (access control) Support both data and function abstractions  Dynamic lookup (function pointers) Definitions of member functions are looked up at runtime Object behavior can change dynamically  Subtyping polymorphism (relations between types) Operations can be applied to multiple types of values  Inheritance (reuse of implementation) Subclasses can modify and inherit behavior of base classes

Encapsulation

 Use access control to support abstractions

 Hide implementation details from outside  Implementation code: operate on data representation  Client code: invoke a fixed set of interface operations  Access control: only a few functions can access private data  Supported by the type system of the language  Example: ML abstypes, C++/Java classes

 Compare to using function closures to support abstractions

 hide implementation detail in function scope  variables can be accessed only by functions within scope  Difference: implementation message Object

Function Objects in C++  ML : functions as first-class objects fun mk_counter (init) = let val count = ref init in let fun counter(inc) = (count := !count+inc; !count) in counter end end; val c = mk_counter(1); c(2) + c(2);  C++ class mk_counter { int count; public: mk_counter (int init) { count = init; } int counter(int inc) { count = count + inc; return count; } } mk_counter c(1); c.counter(2) + c.counter(2);

Dynamic Lookup

 In object-oriented programming,

object->message (arguments) Example: x->add(y)  Code depends on object and message.  Different add if x is integer, complex

 In conventional programming,

Operation(operands) Example: add(x,y)  Meaning of operation is always the same  For example, ML abstype access functions are treated the same as global functions Fundamental difference between ML abstype and C++/Java objects