



























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of abstraction and information hiding in object-oriented programming, including function and data abstractions, modules, and object-orientation. It also covers the use of access control to support abstractions and the difference between static and dynamic lookup.
Typology: Papers
1 / 35
This page cannot be seen from the preview
Don't miss anything!




























Interface, specification, and implementation
Procedural abstraction Abstract data types Packages and modules Generic abstractions Functions and modules with type parameters
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
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:
Nameless data accessed through pattern matching
Function names are bound in enclosing scope C++/Java class:
Every member must have a name
Method names are bound within the class (the class is a scope)
Modules: Combination Of Data And Function Abstractions
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
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);
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() { ... } };
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
Abstractions Information hiding: interface and implementation details Function and data abstractions
Types, variables, constants, functions Interface: declarations visible to the outside
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)
Organize concepts into objects and classes Build extensible systems
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
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
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);
object->message (arguments) Example: x->add(y) Code depends on object and message. Different add if x is integer, complex
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