JavaOOP

Course: CMSC 132, Fall 2005
School: Maryland
Rating:
 
 
 
 
 

Word Count: 1252

Document Preview

132: CMSC Object-Oriented Programming II Java Support for OOP Department of Computer Science University of Maryland, College Park Object Oriented Programming (OOP) OO Principles Abstraction Encapsulation Abstract Data Type (ADT) Implementation independent interfaces Data and operations on data Java Many language features supporting OOP Overview Objects & class, this, super References, alias, levels of...

Register Now

Unformatted Document Excerpt

Coursehero >> Maryland >> Maryland >> CMSC 132

Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.

Course Hero has millions of student submitted documents similar to the one below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
132: CMSC Object-Oriented Programming II Java Support for OOP Department of Computer Science University of Maryland, College Park Object Oriented Programming (OOP) OO Principles Abstraction Encapsulation Abstract Data Type (ADT) Implementation independent interfaces Data and operations on data Java Many language features supporting OOP Overview Objects & class, this, super References, alias, levels of copying Constructor, initialization block Garbage collection, destructor Package, scope, inner classes Modifiers Public, Private, Protected Static, Final, Abstract Generic programming Object & Class Object Abstracts away (data, algorithm) details Encapsulates data Instances exist at run time Class Blueprint for objects (of same type) Exists at compile time "this" Reference Description Reserved keyword Refers to object through which method was invoked Allows object to refer to itself Use 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 } } Inheritance Definition Relationship between classes when state and behavior of one class is a subset of another class Terminology Superclass / parent More general class Subclass More specialized class Forms a class hierarchy Helps promote code reuse "super" Reference Description Reserved keyword Refers to superclass Allows object to refer to methods / variables in superclass Examples super.x super() super.foo() // accesses variable x in superclass // invokes constructor in superclass // invokes method foo() in superclass References & Aliases Reference A way to get to an object, not the object itself All variables in Java are references to objects Alias Multiple references to same object "x == y" operator tests for alias x.equals(y) tests contents of object (potentially) Reference x Object z Reference y Implementing Equals Approach we want to use (assuming class A) public boolean equals(Object obj) { if (obj == this) return true; if (!(obj instanceof A)) return false; A a = (A)obj; /* Specific comparison based on A fields appears here */ } What happens if we use comparisons of Class objects rather than instanceof? Example: See equalsMethod package Cloning Cloning Creates identical copy of object using clone( ) Cloneable interface Supports clone( ) method Returns copy of object Copies all of its fields Does not clone its fields Makes a shallow copy Example: See cloning package Three Levels of Copying Objects Assume y refers to object z 1. Reference copy y z x y z Makes copy of reference x = y; 2. Shallow copy ... Makes copy of object x = y.clone( ); 3. Deep copy ... x z' z z' Makes copy of object z y and all objects (directly or indirectly) referred to by z x ... ... Constructor Description Method invoked when object is instantiated Helps initialize object Method with same name as class w/o return type Default parameterless constructor If no other constructor specified Initializes all fields to 0 or null Implicitly invokes constructor for superclass If not explicitly included Constructor 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 } } Initialization Block Definition Block of code used to initialize static & instance variables for class Motivation Enable complex initializations for static variables Control flow Exceptions Share code between multiple constructors for same class Initialization Block Types Static initialization block Code executed when class loaded Initialization block Code executed when each object created (at beginning of call to constructor) Example class Foo { static { A = 1; } // static initialization block { A = 2; } } // initialization block Variable Initialization Variables may be initialized At time of declaration In initialization block In constructor Order of initialization 1. Declaration, initialization block (in the same order as in the class definition) 2. Constructor Variable Initialization Example class Foo { static { A = 1; } // static initialization block static int A = 2; // static variable declaration static { A = 3; } // static initialization block { B = 4; } // initialization block private int B = 5; // instance declaration variable { B = 6; } // initialization block Foo() { // constructor A = 7; B = 8; } // now A = 7, B = 8 } // initializations executed in order of number Garbage Collection Concepts All interactions with objects occur through reference variables If no reference to object exists, object becomes garbage (useless, no longer affects program) Garbage collection Reclaiming memory used by unreferenced objects Periodically performed by Java Not guaranteed to occur Only needed if running low on memory Destructor Description Method with name finalize() Returns void Contains action performed when object is freed Invoked automatically by garbage collector Not invoked if garbage collection does not occur Usually needed only for non-Java methods Example class Foo { void finalize() { ... } } // destructor for foo Method Overloading Description Same name refers to multiple methods Sources of overloading Multiple methods with different parameters Constructors frequently overloaded Redefine method in subclass Example class Foo { Foo( ) { ... } Foo(int n) { ... } } // 1st constructor for Foo // 2nd constructor for Foo Package Definition Group related classes under one name Helps manage software complexity Separate namespace for each package Package name added in front of actual name Put generic / utility classes in packages Avoid code duplication Example package edu.umd.cs; // name of package Package Import Import Make classes from package available for use Java API java.* (core) javax.* (optional) Example import java.util.Random; // import single class import java.util.*; ... // all classes in package // class definitions Scope Scope Part of program where a variable may be referenced Determined by location of variable declaration Boundary usually demarcated by { } Example public MyMethod1() { int myVar; ... } myVar accessible in method between { } Scope Example Example package edu.umd.cs ; public class MyClass1 { public void MyMethod1() { ... } public void MyMethod2() { ... } } public class MyClass2 { } Scopes Method Method Class Package Class Inner Classes Description Class defined in scope of another class Property Can directly access all variables & methods of enclosing class (including private fields & methods) Example public class OuterClass { private Object value; public class InnerClass { ...Object x = value; } } Modifier Description Java keyword (added to definition) Specifies characteristics of a language construct (Partial) list of modifiers Public / private / protected Static Final Abstract Modifier Examples public class Foo { private static int count; private final int increment = 5; protected void finalize { ... } } public abstract class Bar { abstract int go( ) { ... } }...

Find millions of documents on Course Hero - Study Guides, Lecture Notes, Reference Materials, Practice Exams and more. Course Hero has millions of course specific materials providing students with the best way to expand their education.

Below is a small sample set of documents:

Maryland - ECE - 350
Computer Architecture and Organization: From Software to HardwareManoj Franklin University of Maryland, College Park c Manoj Franklin 2007PrefaceIntroduction Welcome! Bienvenidoes! Bienvenue! Benvenuto! This book provides a fresh introduction to
Maryland - ECE - 312
Home Work 1 Solution3.4 (a) The output will have only the positive portions of the sine wave, i.e., the negative portions will be clipped. Positive peak voltage = 10V Negative peak voltage = 0V (b) The output will have only the negative portions of
Maryland - ECE - 350
g }g Syd y| d1gS|c$}1 d 151 g c g"|d ge|dd11zd 0wd1}0y 5g | 1c d 1 g$ } c g114}d~ gehcg ey8 tc } c
Maryland - ECE - 350
0 g$gd d 0 w 4 }dct d 0 g$gd g d 0 4 tdct z g$gd g c 0 ~ g } g tdct 0 g$gd d 0
Maryland - ECE - 350
ENEE 350 Fall 2004 Practice Problems, Chapter 8 Q1. Design a 10-bit group ripple carry adder (GRCA) that uses 5-bit look ahead carry adder. Draw the logic diagram and write all Boolean equations for implementing such an adder. If the maximum fan in (
Maryland - ECE - 350
k k k x d k t x y d }t { t k l kyh g g {t 7Y {|7h 3 ftCd|5ffCd" l u5rYwYu zoYuC7fuf7Y{ flYf{mlrIYrudl l 7fuf75zz k pC}TmlQ(7rg7$hfjYps"5mzHfhfg7$huflY{lx l~ kx l g yh g g yh k k } k { zt y y } k y} k y j x l~ k l~ kz { y d{ k yt y
Maryland - AMSC - 661
AMSC 661 - HW 1 Due on Thursday February 12 (in class) 1. What are the types of the following equations (a) uxx uxy + 2uy + uyy 3uyx + 4u = 0 (b) 9uxx + 6uxy + uyy + ux = 0. 2. Find the regions in the xy plane where the equation (1 + x)uxx + 2xyuxy
Maryland - MOCB - 639
Vacuole Fusion at a Ring of Vertex Docking Sites Leaves Membrane Fragments within the OrganelleLi Wang, E. Scott Seeley, William Wickner and Alexey J. Merz (2002) Cell 108, 357-69Presented by Ruby Kish & Karen Swanson March 17, 2005OverviewPrevi
Maryland - BSCI - 410
Molecular Genetics Exam 3, Tuesday, Nov. 28, 2006 page 1 of 4 -Your name: _ -1. (10 points). Compare and contrast the initiation of translation in prokaryotes and eukaryotes. Your answer should incorporate information about differences in gene organi
Maryland - HONR - 278
Received 28 January 2002 Accepted 11 March 2002 Published online 22 May 2002Ecomorphological analysis of trophic niche partitioning in a tropical savannah bat communityLuis F. Aguirre1,2* , Anthony Herrel2, R. van Damme2 and E. Matthysen 21 2Ce
Maryland - HONR - 278
Maryland - PHYS - 122
Maryland - PHYS - 122
Maryland - CMSC - 818
Location Awareness and Public SafetyNick Gramsky Ken Knudsen February 12, 2009DomainIndividuals Emergencies can happen at any moment Rapid informed response improves the quality of life Public Safety Security Operations Center (SOC) Currently mon
Maryland - CMSC - 417
cmsc 417 programming assignment twoSeptember 25, 2006Your task in this assignment will be to build and maintain a neighbor table from multicast hello messages.1ContextA hello message tells the receiver Im alive. A recipient uses this hello me
Maryland - CMSC - 132
Great Theoretical Ideas In Computer Science Steven Rudich Lecture 17 Mar 14, 2003 CS 15-251 Spring 2003 Carnegie Mellon UniversityThe Mathematics Of 1950s Dating: Who wins the battle of the sexes?Steven Rudich: www.discretemath.com www.rudich.net
Maryland - BSCI - 410
Lecture 9: Mapping a gene defined by the mutation I. Classical mapping II. Molecular mapping III. Positional cloningRead 387-398 Fig. 11.17; 11.19; 11.20; 11.21; 11.22Self-reviewing of classical genetics Read: 114-117; 123-127 Fig. 5.2; 5.3; 5.4;
Maryland - BSCI - 410
Mapping with a CAPS marker f f E E f F X E eselfNo. of plants No. of recombinant Chr.F e F eE/e: CAPS marker F/f: a mutation causing abnormal flowers Ler: solid line Col: dotted line11 6 00 1 2Recombination FrequencyCE eRecombination F
Maryland - BSCI - 410
Lecture 5: Genetic interactions and epistasisA. Epistasis in a biochemical pathway B. Epistasis in a regulatory pathway C. Additive interactions D. Synergistic interactions E. SuppressionsRead 3.14 (p60-61); 7.23 (p232-234) 8.32 (p290-291); 8.5 (
Maryland - ENEE - 322
Correction to Lecture of Monday, November 19 on System RealizationsI made an error in today's lecture by somehow reversing the coefficient order. The correct version is presented in these notes. Let the system input and output be related by the diff
Maryland - PHIL - 250
Maryland - CMSC - 122
CMSC 122 Quiz 2 WorksheetThe second quiz for the course will be on Wednesday, Feb 11 at the beginning of the class (2:00 pm). The following list provides more information about the quiz: The quiz will be a written quiz (no computer). Closed book,
Maryland - CMSC - 122
Announcements Instructor: Nelson Padua-Perez (nelson@cs.umd.edu) No posting of code in the forum Check class announcements daily You must implement programming projects by yourself1JavaScript Referencehttps:/developer.mozilla.org/en/Core_
Maryland - CMSC - 122
Announcements Instructor: Nelson Padua-Perez (nelson@cs.umd.edu) No posting of code in the forum Check class announcements daily You must implement programming projects by yourself1Designing Using Pseudocode So far we have focused on th
Maryland - ENEE - 140
Laboratory 2: Programming Basics and Variables Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information a. name your program with extension .c b. use o option to specify the name of the executable. For example, gcc hello.c o hel
Maryland - ENEE - 757
The Domain Name SystemAuthor: Paul Mockapetris 1987The old approach: ARPANET : Host name to IP address mapping using a centralized database (i.e., HOSTS.TXT file) maintained by the Network Information Center (NIC) HOSTS.TXT file is ftp-ed daily
Maryland - CMSC - 102
Announcements Project #2 posted. Please start early1Unix Login into linux.grace.umd.edu/solaris.grace.umd.edu Login out (two alternatives) logout exit Commands are case sensitive Listing files ls listing of files in a directory ls -al
Maryland - CMSC - 412
CSMC 412Operating Systems Prof. Ashok K Agrawala 2006 Ashok Agrawala Set 1010.1ObjectivesTo explain the function of file systems To describe the interfaces to file systems To discuss file-system design tradeoffs, including access methods, fil
Maryland - CMSC - 631
Type-Based Flow Analysis: From Polymorphic Subtyping to CFL-ReachabilityJakob Rehof and Manuel FhndrichType-Based Program AnalysisCommon vocabulary Data access paths Function summary Context-sensitivity Directional flow Type-based Type struc
Maryland - CMSC - 433
USENIX COOTS '98April 27, 1998Optimistic Policies: TryingIsolate state into versions E.g. by grouping into a helper class Isolate state changes to atomic commit method that swaps in new state On method entry Save/record current state Apply actio
Maryland - CMSC - 433
Iteration CMSC 433 Programming Language Technologies and Paradigms Fall 2003Iterators and Design Patterns September 25, 2003 Goal: Loop through all objects in an aggregateclass Node { Element elt; Node next; } Node n = .; while (n != null) { .; n