DOC PREVIEW
UMD CMSC 132 - Java Support for OOP

This preview shows page 1-2-3-23-24-25-26-46-47-48 out of 48 pages.

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

Unformatted text preview:

CMSC 132: Object-Oriented Programming IIJava Support for OOPDepartment 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 & class, this, superReferences, alias, levels of copyingConstructor, initialization blockGarbage collection, destructorPackage, scope, inner classesModifiersPublic, Private, ProtectedStatic, Final, AbstractGeneric programmingObject & ClassObjectAbstracts away (data, algorithm) detailsEncapsulates data Instances exist at run timeClassBlueprint for objects (of same type)Exists at compile time“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}}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 superclassReferences & 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 yCloningCloningCreates identical copy of object using clone( )Cloneable interfaceSupports clone( ) methodReturns copy of objectCopies all of its fieldsDoes not clone its fieldsMakes a shallow copyThree Levels of Copying ObjectsAssume y refers to object zyz …yxzz'…yxzz'……1. Reference copyMakes copy of referencex = y;2. Shallow copyMakes copy of objectx = y.clone( );3. Deep copyMakes copy of object z and all objects (directly or indirectly) referred to by zxConstructorDescriptionMethod invoked when object is instantiatedHelps initialize objectMethod with same name as class w/o return typeDefault parameterless constructorIf no other constructor specifiedInitializes all fields to 0 or nullImplicitly 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}}Initialization BlockDefinitionBlock of code used to initialize static & instance variables for classMotivationEnable complex initializations for static variablesControl flowExceptionsShare code between multiple constructors for same classInitialization Block Types Static initialization blockCode executed when class loaded Initialization blockCode executed when each object created (at beginning of call to constructor)Exampleclass Foo {static { A = 1; } // static initialization block { A = 2; } // initialization block}Variable InitializationVariables may be initializedAt time of declarationIn initialization blockIn constructorOrder of initialization1. Declaration, initialization block(in the same order as in the class definition) 2. ConstructorVariable Initialization – Exampleclass Foo {static { A = 1; } // static initialization block static int A = 2; // static variable declarationstatic { A = 3; } // static initialization block { B = 4; } // initialization blockprivate int B = 5; // instance variable declaration{ B = 6; } // initialization blockFoo() { // constructorA = 7;B = 8;} // now A = 7, B = 8} // initializations executed in order of numberGarbage CollectionConceptsAll interactions with objects occur 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}Method OverloadingDescriptionSame name refers to multiple methodsSources of overloadingMultiple methods with different parametersConstructors frequently overloadedRedefine method in subclassExampleclass Foo {Foo( ) { … } // 1stconstructor for FooFoo(int n) { … } // 2ndconstructor for Foo}PackageDefinitionGroup related classes under one nameHelps manage software complexitySeparate namespace for each packagePackage name added in front of actual namePut generic / utility classes in packagesAvoid code duplicationExamplepackage edu.umd.cs; // name of packagePackage – Import ImportMake classes from package available for useJava APIjava.* (core)javax.* (optional)Exampleimport java.util.Random; // import single classimport java.util.*; // all classes in package… // class definitionsScopeScopePart of program where a variable may be referencedDetermined by location of variable declarationBoundary usually demarcated by { } Examplepublic MyMethod1() {int myVar; myVar accessible in... method between { }}Scope – ExampleExamplepackage edu.umd.cs ;public class MyClass1 {public void MyMethod1() {...}public void MyMethod2() {...}}public class MyClass2 {}MethodMethodClassPackageClassScopesInner ClassesDescriptionClass defined in scope of another classPropertyCan directly access all variables & methods of enclosing class (including private fields & methods)Examplepublic class OuterClass {private Object value;public class InnerClass {...Object x = value;}}ModifierDescriptionJava keyword (added to definition)Specifies characteristics of a language construct(Partial) list of modifiersPublic / private / protectedStaticFinalAbstractModifierExamplespublic class Foo


View Full Document

UMD CMSC 132 - Java Support for OOP

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 Java Support for OOP
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 Java Support for OOP 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 Java Support for OOP 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?