ceg860 (prasad)l7class1 classes. ceg860 (prasad)l7class2 class :: instance (object) static structure...

22
ceg860 (Prasad) L7Class 1 Classes

Upload: bennett-manning

Post on 04-Jan-2016

217 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Ceg860 (Prasad)L7Class1 Classes. ceg860 (Prasad)L7Class2 Class :: Instance (Object) Static structure vs Run-time structure (Analogy -- a statue :: Lincoln

ceg860 (Prasad) L7Class 1

Classes

Page 2: Ceg860 (Prasad)L7Class1 Classes. ceg860 (Prasad)L7Class2 Class :: Instance (Object) Static structure vs Run-time structure (Analogy -- a statue :: Lincoln

ceg860 (Prasad) L7Class 2

• Class :: Instance (Object) Static structure vs Run-time structure

(Analogy -- a statue :: Lincoln Memorial)

• Class :: Abstract Data Type A class is an ADT equipped with a possibly

partial implementation.

• Class :: Metaclass A metaclass is a class whose instances are

classes. (Useful for manipulating class representations at run-time by interpreters, browsers, etc.)

Page 3: Ceg860 (Prasad)L7Class1 Classes. ceg860 (Prasad)L7Class2 Class :: Instance (Object) Static structure vs Run-time structure (Analogy -- a statue :: Lincoln

ceg860 (Prasad) L7Class 3

Abstraction : Equivalence Relations– Computability

• Recursive vs Non-recursive

– Semantics• Behavioral Equivalence

– Resource-independent interchangeability

» Performance aspect irrelevant for “correctness”

– E.g., Groups, Fields, Sorting, UNIX, etc

– Complexity (Algorithms)• Time and Space requirements

– Big-Oh (Worst-case Analysis)

– NP-hard vs Polynomial-time

Page 4: Ceg860 (Prasad)L7Class1 Classes. ceg860 (Prasad)L7Class2 Class :: Instance (Object) Static structure vs Run-time structure (Analogy -- a statue :: Lincoln

ceg860 (Prasad) L7Class 4

Specification of Data Types: Motivation

• Precise and unambiguous description.

• Sound and complete description.

• Avoids over-specification.

• Spec: Flexible enough to accommodate all possible implementations.

• Impl.: Constraining enough to exploit representation details for efficiency.

Page 5: Ceg860 (Prasad)L7Class1 Classes. ceg860 (Prasad)L7Class2 Class :: Instance (Object) Static structure vs Run-time structure (Analogy -- a statue :: Lincoln

ceg860 (Prasad) L7Class 5

Specification of Data Types: Details

Type : Values + Operations

Specify

Syntax Semantics

Signature of Ops Meaning of Ops

Model-based Axiomatic(Algebraic)

Description in terms of Give axioms satisfied

standard “primitive” data types by the operations

Page 6: Ceg860 (Prasad)L7Class1 Classes. ceg860 (Prasad)L7Class2 Class :: Instance (Object) Static structure vs Run-time structure (Analogy -- a statue :: Lincoln

ceg860 (Prasad) L7Class 6

ADT Table (symbol table/directory)

empty : Table

update : Key x Info x Table Table

lookUp: Key x Table nfo

lookUp(K,empty) = error

(Use of variable) (Alternative : Use of Preconditions)

lookUp(K,update(Ki, I, T)) =

if K = Ki then I else lookUp(K,T)

(“last update overrides the others”)

Page 7: Ceg860 (Prasad)L7Class1 Classes. ceg860 (Prasad)L7Class2 Class :: Instance (Object) Static structure vs Run-time structure (Analogy -- a statue :: Lincoln

ceg860 (Prasad) L7Class 7

Implementations– Array-based– LinearList-based– Tree-based

• Binary Search Trees, AVL Trees, B-Trees etc

– HashTable-based

• These exhibit a common Table behavior, but differ in performance aspects.

• Correctness of a client program is assured even when the implementation is changed.

Page 8: Ceg860 (Prasad)L7Class1 Classes. ceg860 (Prasad)L7Class2 Class :: Instance (Object) Static structure vs Run-time structure (Analogy -- a statue :: Lincoln

ceg860 (Prasad) L7Class 8

• To make available certain features to all classes, Java defines a root class (class Object) with those features. E.g., Thread support (locks), cloning, equality check, string representation, etc

• Certain class operations are given special status. E.g., creation and initialization of an object is incorporated via constructors.

• Meta-programmingMeta-programming is achieved through appropriate libraries supporting Reflection and Introspection. E.g., In Java 1.1, package java.lang.reflect.

Java approach to metaclass benefits

Page 9: Ceg860 (Prasad)L7Class1 Classes. ceg860 (Prasad)L7Class2 Class :: Instance (Object) Static structure vs Run-time structure (Analogy -- a statue :: Lincoln

ceg860 (Prasad) L7Class 9

Class as module and type

– Module (unit of software decomposition)• Syntactic: Only affects the form of software text, not

what the software can do. Separately compilable.

– Type (static description of dynamic objects)• Semantic: Influences execution of a software system

by defining objects manipulated at run-time.

The facilities provided by a class, viewed as a module, are precisely the operations on instances of the class.

Page 10: Ceg860 (Prasad)L7Class1 Classes. ceg860 (Prasad)L7Class2 Class :: Instance (Object) Static structure vs Run-time structure (Analogy -- a statue :: Lincoln

ceg860 (Prasad) L7Class 10

Example : class Point

• Features/Members – Attributes/Fields (represented using memory)– Routines/Methods (represented using computation)

real x-coord, y-coord; real radius, angle;real distance(Point);void translate(real x,y);

Page 11: Ceg860 (Prasad)L7Class1 Classes. ceg860 (Prasad)L7Class2 Class :: Instance (Object) Static structure vs Run-time structure (Analogy -- a statue :: Lincoln

ceg860 (Prasad) L7Class 11

FeatureFeature Classification by Role

ProcedureProcedure

FunctionFunction

FunctionFunction

Attribute Attribute (Field)(Field)

No result Returns result

Arguments No Arguments

Computation Memory

RoutineRoutine

Page 12: Ceg860 (Prasad)L7Class1 Classes. ceg860 (Prasad)L7Class2 Class :: Instance (Object) Static structure vs Run-time structure (Analogy -- a statue :: Lincoln

ceg860 (Prasad) L7Class 12

FeatureFeature Classification by Implementation

Computation Memory

ProcedureProcedure FunctionFunction

No result Result

RoutineRoutine(Method)(Method) AttributeAttribute

(Field)(Field)

Page 13: Ceg860 (Prasad)L7Class1 Classes. ceg860 (Prasad)L7Class2 Class :: Instance (Object) Static structure vs Run-time structure (Analogy -- a statue :: Lincoln

ceg860 (Prasad) L7Class 13

Uniform Access principle• A client should be able to access a property

(feature) of an object using a single notation whether the property is implemented by memory or by computation.– EiffelEiffel : 0-ary routine f and attribute f have the

same syntax. Attributes cannot be directly assigned.

– JavaJava: 0-ary method syntax is f() (not f). In SmallTalkSmallTalk/Java Beans,Java Beans, a field is accessed via getF() and/or setF(valueType) methods.

Page 14: Ceg860 (Prasad)L7Class1 Classes. ceg860 (Prasad)L7Class2 Class :: Instance (Object) Static structure vs Run-time structure (Analogy -- a statue :: Lincoln

ceg860 (Prasad) L7Class 14

Object-Oriented Style of Computation

• Single Target Principle– Every operation is relative to a certain object.– E.g., pt1.distance(pt2)

• Current Instance– May be implicit, or referred to explicitly using a

keyword such as this (Java/C++), self (Self), Current (Eiffel), etc.

• Module-Type Identification

Page 15: Ceg860 (Prasad)L7Class1 Classes. ceg860 (Prasad)L7Class2 Class :: Instance (Object) Static structure vs Run-time structure (Analogy -- a statue :: Lincoln

ceg860 (Prasad) L7Class 15

double distance( Point p ) { if (p =/= this) { return Math.sqrt( (x - p.x) * (x - p.x) + (y - p.y) * (y - p.y) ); } else { return 0; }}

Page 16: Ceg860 (Prasad)L7Class1 Classes. ceg860 (Prasad)L7Class2 Class :: Instance (Object) Static structure vs Run-time structure (Analogy -- a statue :: Lincoln

ceg860 (Prasad) L7Class 16

Composition : Client and Supplier

• A class C which contains a declaration of the form S x is said to be a client of S. S is said to be a supplier (server) of C.

•x may be an attribute or a function of C, or a local entity or argument of a routine of C.

Page 17: Ceg860 (Prasad)L7Class1 Classes. ceg860 (Prasad)L7Class2 Class :: Instance (Object) Static structure vs Run-time structure (Analogy -- a statue :: Lincoln

ceg860 (Prasad) L7Class 17

Feature Calls

• Qualified calls– x.f or x.f()– x.f(a,b,…)

• Unqualified calls– f or f()– f(a,b,…)

Equivalently, – this.f or this.f()– this.f(a,b,…)

• Operators• infix

– associativity

• prefix

– Operator definitions supported in order to be consistent with the standard mathematical notations.

– A * B, 5, etc.

Page 18: Ceg860 (Prasad)L7Class1 Classes. ceg860 (Prasad)L7Class2 Class :: Instance (Object) Static structure vs Run-time structure (Analogy -- a statue :: Lincoln

ceg860 (Prasad) L7Class 18

Selective Export and Information Hiding

• EiffelEiffel has a general mechanism to export a feature to designated clients and their proper descendants.

• JavaJava and C++C++ support three levels: – public : all clients.

– private : (all instances of) current class.

– protected : all subclasses.– Default : package wide visibility.

Page 19: Ceg860 (Prasad)L7Class1 Classes. ceg860 (Prasad)L7Class2 Class :: Instance (Object) Static structure vs Run-time structure (Analogy -- a statue :: Lincoln

ceg860 (Prasad) L7Class 19

Function In-lining in C++ – Interface file ( “.h” extension ).

• Contains prototype declarations, and (short) function definitions that may be in-lined.

– Implementation file ( “.c++” extension ).• Contains function definitions, possibly with inline directive.

• In-lining enables programmers to have benefits of encapsulation while avoiding some of the run-time costs of function invocation.

Page 20: Ceg860 (Prasad)L7Class1 Classes. ceg860 (Prasad)L7Class2 Class :: Instance (Object) Static structure vs Run-time structure (Analogy -- a statue :: Lincoln

ceg860 (Prasad) L7Class 20

Genericity• Unconstrained

Type parameterized classes Generic type : List[G] Instantiations: List[int], List[String], List[Window], etc

• Constrained Types with operations

Constrained Genericity: Ordered, Group, etc Java interface types.

Page 21: Ceg860 (Prasad)L7Class1 Classes. ceg860 (Prasad)L7Class2 Class :: Instance (Object) Static structure vs Run-time structure (Analogy -- a statue :: Lincoln

ceg860 (Prasad) L7Class 21

type INTMAT is array ( POSITIVE range <>, POSITIVE range <>) of integer;

function int_sum_all( M : INTMAT ) return integer issum : integer := 0;

begin for I in M'range(1) loop

for J in M'range(2) loop sum := sum + M(I,J); end loop; end loop; return (sum);end;

Page 22: Ceg860 (Prasad)L7Class1 Classes. ceg860 (Prasad)L7Class2 Class :: Instance (Object) Static structure vs Run-time structure (Analogy -- a statue :: Lincoln

ceg860 (Prasad) L7Class 22

generic type ET is private; type INDEX is (<>);

ID : ET; type AR is array ( INDEX range <>, INDEX range <>) of ET; with function "+" ( X,Y : ET ) return ET is <>;function sum_all( M : AR ) return ET;

function sum_all( M : AR ) return ET issum : ET := ID;

begin for I in M'range(1) loop

for J in M'range(2) loop sum := sum + M(I,J); end loop; end loop; return (sum);end;