DOC PREVIEW
UMD CMSC 132 - OOP in Java

This preview shows page 1-2-24-25 out of 25 pages.

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

Unformatted text preview:

OOP in JavaObject Oriented Programming (OOP)OverviewObject & ClassReferences & AliasesReferences & Aliases – Issues“this” Reference“this” Reference – ExampleInheritance“super” ReferenceConstructorConstructor – ExampleGarbage CollectionDestructorMethod OverloadingModifierSlide 17Visibility ModifierSlide 19Modifier – StaticModifier – FinalSlide 22Slide 23Modifier – AbstractInterfaceOOP in JavaFawzi EmadChau-Wen TsengDepartment of Computer ScienceUniversity of Maryland, College ParkObject Oriented Programming (OOP)OO PrinciplesAbstractionEncapsulation Abstract Data Type (ADT)Implementation independent interfacesData and operations on dataJavaMany language features supporting OOPOverviewObjects & classReferences & alias“this” & “super” referenceConstructorGarbage collection & destructorModifiersPublic, Private, ProtectedStatic FinalObject & ClassObjectAbstracts away (data, algorithm) detailsEncapsulates data Instances exist at run timeClassBlueprint for objects (of same type)Exists at compile timeReferences & AliasesReferenceA way to get to an object, not the object itselfAll variables in Java are references to objectsAliasMultiple references to same object“X == Y“ operator tests for alias X.equals(Y) tests contents of object (potentially)Object ZReference XReference YReferences & Aliases – IssuesCopyingReferencesX = new Object();Y = X; // Y refers to same object as XObjectsX = new Object();Y = X.clone(); // Y refers to different objectModifying objectsX = new Object();Y = X;X.change(); // modifies object for Y“this” ReferenceDescriptionReserved keywordRefers to object through which method was invokedAllows object to refer to itselfUse to refer to instance variables of object“this” Reference – Example class Node {value val1;value val2;void foo(value val2) { … = val1; // same as this.val1 (implicit this) … = val2; // parameter to method … = this.val2; // instance variable for object bar( this ); // passes reference to object }}InheritanceDefinitionRelationship between classes when state and behavior of one class is a subset of another classTerminologySuperclass / parent  More general classSubclass  More specialized classForms a class hierarchyHelps promote code reuse“super” ReferenceDescriptionReserved keywordRefers to superclassAllows object to refer to methods / variables in superclassExamplessuper.x // accesses variable x in superclasssuper() // invokes constructor in superclasssuper.foo() // invokes method foo() in superclassConstructorDescriptionMethod invoked when object is instantiatedHelps initialize objectMethod with same name as class w/o return typeImplicitly invokes constructor for superclassIf not explicitly includedConstructor – Example class foo { foo() { … } // constructor for foo } class bar extends foo { bar() { // constructor for bar // implicitly invokes foo() here … } } class bar2 extends foo { bar2() { // constructor for bar super(); // explicitly invokes foo() here } }Garbage CollectionConceptsAll interactions with objects occurs through reference variablesIf no reference to object exists, object becomes garbage (useless, no longer affects program)Garbage collectionReclaiming memory used by unreferenced objectsPeriodically performed by JavaNot guaranteed to occurOnly needed if running low on memoryDestructorDescriptionMethod with name finalize()Returns void Contains action performed when object is freedInvoked automatically by garbage collectorNot invoked if garbage collection does not occurUsually needed only for non-Java methodsExample class foo { void finalize() { … } // destructor for foo }Method OverloadingDescriptionSame name refers to multiple methodsSources of overloadingMultiple methods with different parametersConstructors frequently overloadedRedefine method in subclassExample class foo { foo() { … } // constructor for foo foo(int n) { … } // 2nd constructor for foo }ModifierDescriptionJava keyword (added to definition)Specifies characteristics of a language construct(Partial) list of modifiersPublic / private / protectedStaticFinalAbstractModifierExamplepublic class foo { private static int count; private final int increment = 5; protected void finalize { … }}public abstract class bar { abstract int go() { … }}Visibility ModifierPropertiesControls access to class membersApplied to instance variables & methodsTypesPublicMay be directly referenced outside objectPrivateReferenced only within class definitionProtectedReferenced within class definition & by subclassesVisibility ModifierFor instance variablesShould usually be private to enforce encapsulationSometimes may be protected for subclass accessFor methodsPublic methods – provide services to clientsPrivate methods – provide support other methodsProtected methods – provide support for subclassModifier – StaticStatic variableSingle copy for classShared among all objects of class Static methodCan be invoked through class nameDoes not need to be invoked through objectCan be used even if no objects of class existCan not reference instance variablesModifier – FinalFinal variableValue can not be changedMust be initialized in every constructorAttempts to modify final are caught at compile time Final static variableUsed for constantsExamplefinal static int Increment = 5;Modifier – FinalFinal methodMethod can not be overloaded by subclassPrivate methods are implicitly finalFinal classClass can not be a superclass (extended)Methods in final class are implicitly finalModifier – FinalUsing final classesPrevents inheritance / polymorphismMay be useful for SecurityObject oriented designExample – class String is finalPrograms can depend on properties specified in Java library APIPrevents subclass that may bypass security restrictionsModifier – AbstractDescriptionRepresents generic conceptCan not be instantiatedAbstract classPlaceholder in class hierarchyCan be partial description of classCan contain non-abstract methodsRequired if any method in class is abstract Exampleabstract class foo { // abstract class abstract void bar() { … } // abstract methodInterfaceDescriptionCollection ofConstantsAbstract methodsCan not be instantiatedClasses can implement interfaceMust implement all methods in interfaceExampleclass foo implements bar { … } // interface


View Full Document

UMD CMSC 132 - OOP in Java

Documents in this Course
Notes

Notes

8 pages

Recursion

Recursion

12 pages

Sorting

Sorting

31 pages

HTML

HTML

7 pages

Trees

Trees

19 pages

HTML

HTML

18 pages

Trees

Trees

19 pages

Honors

Honors

19 pages

Lecture 1

Lecture 1

11 pages

Quiz #3

Quiz #3

2 pages

Hashing

Hashing

21 pages

Load more
Download OOP in Java
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 OOP in Java 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 OOP in Java 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?