DOC PREVIEW
UT Arlington CSE 3302 - Lecture 16 - Objects

This preview shows page 1-2-15-16-31-32 out of 32 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CSE 3302Lecture'16:'Objects25'March'2010Nate'NystromUniversity'of'Texas'at'ArlingtonTuesday, March 30, 2010TypesLast'Cme:strongly)typed'if'no'type'errors'occur'at'run'Cmesta.cally)typed)languages'rule'out'type'errors'at'compile'Cme◾requires'programmers'to'write'type'declaraCons◾sound)type)systems'ensure'at'compile'Cme'that'no'type'errors'will'occur'at'run'Cme◾must'be'conservaCvedynamically)typed)languages'rule'out'type'errors'at'run'Cme◾dynamic'checks'before'operaCons2Tuesday, March 30, 2010RecordsLast'Cme,'discussed'records:◾{x1:'T1,'...,'xn:'Tn}Type:◾{x:'int,'s:'String,'c:'char,'y:'int'}ConstrucCon:◾{x'='2,'s'='“hi”,'c'='‘x’,'y'='10'}SelecCon◾r.y3Tuesday, March 30, 2010Records as ADTsstruct'IntList'{''''int'head;''''struct'IntList'*tail;'''//'must'be'a'pointer!}4Tuesday, March 30, 2010Records as ADTsstruct'IntList'{''''int'head;''''struct'IntList'*tail;'''//'must'be'a'pointer!}int'get(IntList'*xs,'int'i)'{'''''if'(i'=='0)'return'xs[>head;'''''else'get(xs[>tail,'i[1);'}void'append(IntList'*xs,'IntList'*ys)'{''''if'(xs[>tail'=='NULL)'xs[>tail'='ys;''''else'append(xs[>tail,'ys);'}5Tuesday, March 30, 2010ProblemsImplementaCon'exposed.Only'one'implementaCon'supported.Clients'of'the'ADT'must'all'u se'same'implementaCon.6Tuesday, March 30, 2010ObjectsFocus'is'on'data'rather'than'on'processes.Programs'composed'of'self[contained,'interacCng'objects.◾vs.'as'a'list'of'tasks'to'performBundle'data'with'the'operaCons'on'that'dataEncapsulate'(hide)'implementaCon'from'clients.◾Can'only'access'data'through'public'interface.7Tuesday, March 30, 2010ObjectsSource'code'for'class'defines'concrete'type'(implementaCon)Interface'defined'by'public'variables'and'methods'of'a'classclass'IntList'{''''private'int'head;'private'IntList'tail;''''public'int'get(int'i)'{''''''''if'(i'=='0)'return'this.head;'else'return'get(this.tail,'i[1);'}''''public'void'append(IntList'ys)'{''''''''if'(this.tail'=='null)'th is.tail'='ys;'else'this.tail.append(ys);'}'}8Tuesday, March 30, 2010OO languages9Simula'67SmalltalkC++CSelfObjecCve'CC#JavaRubyOCamlMLJavaScriptLISPCLOSScalaNot'an'exhau sCve'su rveyTuesday, March 30, 2010OO languagesTwo'kinds'of'OO'languages◾class[based◾Simula,'Smalltalk,'C++,'Java,'C#,'Scala◾prototype[based◾Self,'Cecil,'JavaScript10Tuesday, March 30, 2010Class-based languagesA'class'defines'a'template'fo r'creaCng'objects◾defines'what'members'an'object'of'class'C'(“instance”)'has◾fields'(aka'member'variables,'instance'variables)◾methods'(aka'member'funcCons)All'instances'have'the'same'structure◾same'methods,'same'fieldsWe’ll'ignore'staCc'members'for'now11Tuesday, March 30, 2010Inheritance of classesA'subclass'(derived'class)'in herits'from'(extends)'one'or'more'superclasses'(base'class)As'if'members'of'the'superclass'were'copied'down'into'the'subclass'declaraConInstance'of'subclass'contains'all'members'of'an'instance'of'the'supercl ass'+'n ew'members'defined'by'the'subclass12Tuesday, March 30, 2010Javaclass'Point'{''''int'x,'y;''''void'move(int'dx,'int'dy)'{''''''''x'+='dx;'y'+='dy;'}}class'ColorPoint'extends'Point'{''''Color'c;''''void'redden()'{''''''''c'='Color.RED;'}}13xyxycTuesday, March 30, 2010Prototype-based languagesNo'noCon'of'classInheritance'is'by'delega.onTo'create'a'new'object:◾create'an'object'from'nothing'(ex'nihilo),'or◾clone'another'object'(the'prototype)◾modify'the'new'object◾maintain'a'reference'(delegate)'to'the'original14Tuesday, March 30, 2010JavaScript//'create'two'objectsvar'p'='{ x:'1,'y:'2};var'cp'='{ color:'“red” };//'make'cp'extend'pcp.__proto__'='p;cp.x'//'1cp.y'//'2cp.c'//'“red”15Tuesday, March 30, 2010DelegationIf'object'does'not'contain'a'given'field,'check'its'prototypeBehaves'similarly'to'inheritance16Tuesday, March 30, 2010Multiple implementationsOO'languages'let'you'have'mulCple'implementaCons'of'the'same'specificaCon:class'List'{''''int'length();''''int'get(int'i);''''List'append(List'x);}class'ArrayList'extends'List'{'...'}class'LinkedList'extends'List'{ '.. .'}class'ConcList'extends'List'{ '... '}17Tuesday, March 30, 2010Dispatching problemProblem:'don’t'know'what'code'to'run'at'compile'CmeList'a'='...;a.length ();◾ArrayList.length'or'LinkedList.length?◾Objects'must'“know”'their'implementaCon'at'run'Cme18Tuesday, March 30, 2010Compiling objectsAdd'to'each'object'an'extra'pointer'to'a'dis patch)vector'(aka'virtual'table,'vtable)'with'pointers'to'method'codeCode'receiving'x':'List'only'knows'x'has'an'iniCal'dispatch'vector'pointer19headtaillenarrayLinkedList.lengthLinkedList.getLinkedList.appendArrayList.lengthArrayList.'getArrayList.'append?.length?. get?.appendTuesday, March 30, 2010PolymorphismCode'can'use'values'with'more'than'one'typeObject'oriented'languages'support'subtype)polymorphismGood'for'heterogeneous'data'structures'containing'different'implementaCons'of'the'same'interfaceCan'mix'different'Animal'i mplementaCons'in'the'same'list◾(Cat,'Duck,'Cow,'Moose,'TRex,'Human,'Sponge)20Tuesday, March 30, 2010Type relationshipsClasses'and'their'superclasses'are'related'by'a'subtype'relaConship◾ArrayList'<:'List◾LinkedList'<:'List21LinkedList ArrayListListTuesday, March 30, 2010SubtypesOne'type'extends'another'by'allowing'more'operaConsclass'Point'{''''int'x();''''int'y();}class'ColorPoint'{''''int'x();''''int'y();''''Color'color();}22ColorPointPointColorPoint'<:'Point“is'a'subtype'of”(also:'≤)Tuesday, March 30, 2010SubtypingPredicate'view'of'types◾A'type'is'a'predicate'on'values◾T1'is'a'subtype'of'T2'if'T1’s'predicate'implies'T2’s◾Barney'is'a'Dinosaur'=>'Barney'is'an'Animal'Set[theoreCc'view'of'types◾A'type'is'a'set'of'values◾T1'is'a'subtype'of'T2'if'T1’s'set'of'values'is'a'subset'of'T2’s◾The'set'of'Dinosaurs'is'a'subset'of'the'set'of'Animals◾Note:'it’s'a'subset'of'values'not'a'subset'of'opera.ons23Tuesday, March 30, 2010Substitution principleCan'always'subsCtute'an'instance'of'a'subtype'for'an'instance'of'a'supertype'and'the'program'will'have'no'type'errorsBird''''[[[>'''''PenguinLiskov'SubsCtuCon'Principle:◾if'P(x)'is'true'about'objects'x'of'type'T,'then'P(y)'is'true'for'objects'y'of'type'S,'a'subtype'of'T◾“behavioral'subtyping”◾usually'too'strong'to'be'enforceable24Tuesday, March 30, 2010Subtype relationSubtyping'is'a'binary)rela.on'on'typesNotaCon:◾T1'<:'T2'''–'''T1'is'a'subtype'of'T2<:'is:◾reflexive:'''''''''''''T'<:'T◾transiCve:'''''''''''if'T 1'<:'T2'and'T


View Full Document

UT Arlington CSE 3302 - Lecture 16 - Objects

Documents in this Course
Smalltalk

Smalltalk

11 pages

Syntax

Syntax

5 pages

Syntax

Syntax

5 pages

JAVA

JAVA

57 pages

Semantics

Semantics

41 pages

Control

Control

74 pages

Load more
Download Lecture 16 - Objects
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 16 - Objects and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 16 - Objects 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?