DOC PREVIEW
UT Arlington CSE 3302 - Lecture Notes

This preview shows page 1-2-3-4-5-37-38-39-40-41-42-75-76-77-78-79 out of 79 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 79 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 79 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 79 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 79 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 79 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 79 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 79 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 79 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 79 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 79 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 79 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 79 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 79 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 79 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 79 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 79 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 79 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CSE 3302Lecture'17:'More'objects30'March'2010Nate'NystromUniversity'of'Texas'at'ArlingtonThursday, April 1, 2010Review of OO conceptsObjects'bundle'data'with'the'operaFons'on'the'dataObjects'provide'encapsulaFonClassHbased'and'prototypeHbased'languagesCode'reuse'through'inheritance2Thursday, April 1, 2010Inheritance and subtypingOO'languages'provide'subtype(polymorphism◾A'instance'of'a'subtype'can'be'used'anywhere'an'instance'of'a'supertype'can'be'usedRequires:'any'operaFon'on'sup ertype'must'be'supported'on'a'subtype◾Inheritance'achieves'this:◾a'subclass'is'also'subtype'(usually)◾a'subtype'should'support'more'operaFons'than'a'supertype3Thursday, April 1, 2010“is a”OKen,'subtyping'implies'an'“is'a”'relaFonship◾A'Penguin'is)a'BirdBut:◾A'Stack'is)a'List◾However,'a'Stack'supports'fewer'operaFons'than'List◾A'Stack'is'not'a'subtype'of'List4Thursday, April 1, 2010Stack and ListStack'<:'List'?◾a'stack'is'a'list,'but'with'some'restricFonsList'<:'Stack'?◾a'list'has'more'operaFons'than'a'stack'–'can'use'a'stack'as'a'list“is'a”'isn’t'always'the'“right”'way'to'decide'when'to'use'inheritanceMany'OO'programs'ov eruse(inheritanceOKen'beXer'to'use'containmen t◾Stack'should'contain'a'List––can'hide'operaFons'on'the'Li st◾Stack'should'not'be'a'subtype'of'List5Thursday, April 1, 2010Java collections libraryStack'is'a'subtype'of'List◾Broken:'allows'a'Stack'to'be'used'in'a'nonHstackHlike'wayUnmodifiable'collecFons◾throws'UnsupportedOperaFonExcepFon'if'try'to'write'to'the'collecFon6Thursday, April 1, 2010Virtual methodsIn'C++'methods'may'be'declared'virtual.In'Java,'all '(n onHstaFc)'methods'are'virtual.Which'method'body'to'run'depends'on'receiver’s'runHFme'classclass%A%{%void%m();%}class%B%extends%A%{%void%m();%}A%x%=%new%B();x.m();%//%invokes%B.m()A%y%=%new%A();y.m();%//%invokes%A.m()7Thursday, April 1, 2010Self (aka this)Virtual'methods'have'a'formal'parameter'for'the'method(receiver◾Type'of'the'receiver'is'the'enclosing'classIn'C++,'Java,'C#,'the'receiver'is'an'implicit'parameter'named'thisIn'Smalltalk,'Ruby,'receiver'is'an'implicit'parameter'named'selfIn'Python'and'some'other'languages,'receiver'is'explicitly'named.8Thursday, April 1, 2010Fields are not virtualIn'C++'and'Java,'field'access'is'not'virtual◾which'field'to'use'depends'o n'the'staFc'type'of'the'targetclass%A%{%int%f;%}class%B%extends%A%{%int%f;%}A%x%=%new%B();x.f;%%//%accesses%A.fA%y%=%new%A();y.f;%//%accesses%A.fB%z%=%new%B();z.f;%//%accesses%B.f9Thursday, April 1, 2010ConstructorsConstructors'are'procedures'that'iniFalize'objects.Job'of'constructor'is'to'establish'the'object(invariant.Invariant'can'be'assumed'on'entry'to'any'nonHprivate'method'and'must'be'reestablished'on 'return.Constructor'for'a'subclass'invokes'constructors'for'superclasses,'passing'arguments'up'to'iniFalize'fields.10Thursday, April 1, 2010Java constructor anomalyclass%C%{%%%%final%int%f;%%%%C()%{%init();%f%=%1;%}%%%%void%init()%{%}}class%D%extends%C%{%%%%D()%{%super();%}%%%%void%init()%{%println(f);%}}new%D()%%%//%prints%what?11Thursday, April 1, 2010Java constructor anomalyclass%C%{%%%%final%int%f;%%%%C()%{%init();%f%=%1;%}%%%%void%init()%{%}}class%D%extends%C%{%%%%D()%{%super();%}%%%%void%init()%{%println(f);%}}new%D()%%%//%prints%012Thursday, April 1, 2010EncapsulationObjects'provide'encapsula7on◾Can'hide'implementaFon'details◾Use'the'object'only'through'a'narrow'interface13Thursday, April 1, 2010VisibilityPublic'members'are'visible'everywherePrivate'members'are'visible'only'to'members'of'the'same'classProtected'members'are'visible'to'the'current'class'and'to'subclasses'onlyJava:◾packageHscoped'(default)'and'also(protected'members'are'visible'to'other'classes'in'the'same'package14Thursday, April 1, 2010ProblemInheritance'violates'encapsulaFon!Subclasses'know'implementaFon'details'of'superclassesBut,'also'requires(subclasses'to'know'the'implementaFon'details'of'their'superclassesClass'provides'two'interfaces:◾one'interface'for'external'users◾one'interface'for'subclasses15Thursday, April 1, 2010Examplejava. lan g.ProperFes'inherits'from'java.lang.Hashtable“Because(Proper-es(inherits(from(Hashtable,(the(put(and(putAll(methods(can(be(applied(to(a(Proper-es(object.(Their(use(is(strongly(discouraged(as(they(allow(the(caller(to(insert(entries(whose(keys(or(values(are(not(Strings.(The(setProperty(method(shoul d(b e(used(in stead.(If(the(store(or(save(method(is(called(on(a(“compromised”(Proper-es(object(that(contains(a(nonEString(key(or(value,(the(call(will(fail.”16Thursday, April 1, 2010Abstract classes and methodsMethods'can'be'declared'abstract'(pure'virtual)◾Java:'abstract'void'm();◾C++:''virtual'void'm()'='0;Abstract'methods'don’t'have'a'body.A'class'with'an'abstract'method'must'also'be'abstract.An'abstract'class'cannot'be'instanFated.=>'NonHabstract'classes'must'override'abstract'methods'they'inherit.17Thursday, April 1, 2010Multiple inheritanceA'class'may'have'more'than'one'superclassEffect'is'sFll'as'if'members'are'copiedJava'sup ports'o nly'mul Fpl e'inh eritance'of'interfacesC++'supports'arbitrary'mulFple'inheritance◾causes'all'sorts'of'complicaFonsOther'languages'have'restricted'MI'to'simplify'semanFcs18Thursday, April 1, 2010MI problem 1: ambiguous methodsclass%A%{%void%m()%}class%B%{%void%m()%}class%C%extends%A,%B%{%}C'inherits'an'm()'from'A'and'an'm()'from'B.C++:'caller'must'specify'which'to'use:◾C%*x%=%new%C;◾xI>A::m();◾xI>B::m();19Thursday, April 1, 2010MI problem 1: ambiguous methodsinterface%A%{%void%m()%}interface%B%{%void%m()%}class%C%implements%A,%B%{%void%m()%{%S%}%}In'Java,'mul Fpl e'interface'inheritance'is'okayDon’t'inherit'more'than'one'implementaFonC'is'required'to'implement'm()20Thursday, April 1, 2010MI problem 1(b): ambiguous fieldsclass%A%{%int%f;%}class%B%{%int%f;%}class%C%extends%A,%B%{%}C'inherits'A.f'and'B.f.This'is'okay'because'field'access'is'nonvirtual .Only'issue'is'in'C,'accessing'this.f'is'ambi guou s.◾thisH>A::f''vs.''thisH>B::f21Thursday, April 1, 2010MI problem 2: diamondsclass%A%{%int%f;%}class%B%extends%A%{%}class%C%extends%A%{%}class%D%extends%B,%C%{%}Does'D'have'one'copy'of'f'or'two?22Thursday, April 1, 2010MI problem 2: diamondsC++'supports'virtual(inheritanceclass%A%{%int%f;%}class%B%:%virtual%A%{%}class%C%:%virtual%A%{%}class%D%:%B,%C%{%}D'shares'one'copy'of'A’s'members.Annoying'problem:'B'and'C'are'declared'to'use'virtual'inheritance.'Have'to'anFcipate'that'B'and'C'will'be'mulFply'inherited.23Thursday, April 1,


View Full Document

UT Arlington CSE 3302 - Lecture Notes

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 Notes
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 Notes 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 Notes 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?