module-object - Modularity and Objectoriented Abstractions...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Modularity and Object- oriented Abstractions Encapsulation, dynamic binding, subtyping and inheritance
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 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
Background image of page 2
3 Modularity: Structured Programming When we program, we try to solve a problem by Step1: decompose the problem into smaller sub-problems Step2: try to solve each sub-problem separately Each solution is a separate component that includes Interface: types and operations visible to the outside Specification: intended behavior and property of interface Implementation: data structures and functions hidden from outside Example: a banking program Main program Create account Deposit/Withdraw Print statement
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
4 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
Background image of page 4
5 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 ((y*y)<x) y=y+step; else y=y-step; step = step/2; } return y; }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
6 Example: A Data Abstraction abstype in ML Hide representation of the data structure Syntax abstype <name> = <constructor> of <type> with <declaration> … <declaration> end abstype complex = C of real*real with fun complex(x,y:real) = C(x,y) fun x_coord(C(x,y)) = x fun y_coord(C(x,y)) = y fun add(C(x1,y1),C(x2,y2)) = C(x1+x2,y1+y2) end C(x,y) can appear only in implementation of complex No other operations can use C(x,y)
Background image of page 6
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
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 34

module-object - Modularity and Objectoriented Abstractions...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online