New version page

UMD CMSC 132 - OOP in Java

Documents in this Course
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
Upgrade to remove ads

This preview shows page 1-2-3-4 out of 13 pages.

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

Upgrade to remove ads
Unformatted text preview:

1OOP 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 OOP2OverviewObjects & 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 time3References & 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 Y4“this” ReferenceDescriptionReserved keywordRefers to object through which method was invokedAllows object to refer to itselfUse to refer to instance variables of object“this” Reference – Exampleclass 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 objectbar( this ); // passes reference to object}}5InheritanceDefinitionRelationship 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 superclass6ConstructorDescriptionMethod invoked when object is instantiatedHelps initialize objectMethod with same name as class w/o return typeImplicitly invokes constructor for superclassIf not explicitly includedConstructor – Exampleclass foo {foo() { … } // constructor for foo}class bar extends foo {bar() { // constructor for bar// implicitly invokes foo() here…}}class bar2 extends foo {bar2() { // constructor for barsuper(); // explicitly invokes foo() here}}7Garbage 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 methodsExampleclass foo {void finalize() { … } // destructor for foo}8Method OverloadingDescriptionSame name refers to multiple methodsSources of overloadingMultiple methods with different parametersConstructors frequently overloadedRedefine method in subclassExampleclass foo {foo() { … } // constructor for foofoo(int n) { … } // 2ndconstructor for foo}ModifierDescriptionJava keyword (added to definition)Specifies characteristics of a language construct(Partial) list of modifiersPublic / private / protectedStaticFinalAbstract9ModifierExamplepublic 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 subclasses10Visibility 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 variables11Modifier – 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 final12Modifier – 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 classabstract void bar() { … } // abstract method13InterfaceDescriptionCollection ofConstantsAbstract methodsCan not be instantiatedClasses can implement interfaceMust implement all methods in interfaceExampleclass foo implements bar { … } // interface


View Full Document
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?