

























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Padua-Perez; Class: OBJECT-ORIENTED PROG II; Subject: Computer Science; University: University of Maryland; Term: Spring 2007;
Typology: Study notes
1 / 33
This page cannot be seen from the preview
Don't miss anything!


























Object Oriented Programming (OOP)
Abstraction Encapsulation
Implementation independent interfaces Data and operations on data
Many language features supporting OOP
Object & Class
Abstracts away (data, algorithm) details Encapsulates data Instances exist at run time
Blueprint for objects (of same type) Exists at compile time
References & Aliases
A way to get to an object, not the object itself All variables in Java are references to objects
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
“this” Reference
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
“super” Reference
Reserved keyword Refers to superclass Allows object to refer to methods / variables insuperclass
super.x
// accesses variable x in superclass
super()
// invokes constructor in superclass
super.foo()
// invokes method foo() in superclass
Constructor
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
Initialization Block
Block of code used to initialize static & instancevariables for class
Enable complex initializations for static variables
Control flow Exceptions
Share code between multiple constructors forsame class
Initialization Block Types
Code executed when class loaded
Code executed when each object created (at beginning of call to constructor)
static { A = 1; }
// static initialization block
// initialization block
Variable Initialization – Example
class Foo {
static { A = 1; }
// static initialization block
static int A = 2;
// static variable declaration
static { A = 3; }
// static initialization block
// initialization block
private int B = 5;
// instance variable declaration
// initialization block
Foo() {
// constructor
// now A = 7, B = 8
// initializations executed in order of number
Garbage Collection
All interactions with objects occur throughreference variables If no reference to object exists, object becomesgarbage (useless, no longer affects program)
Reclaiming memory used by unreferenced objects Periodically performed by Java Not guaranteed to occur Only needed if running low on memory
Method Overloading
Same name refers to multiple methods
Multiple methods with different parameters
Constructors frequently overloaded
Redefine method in subclass
Foo( ) { … }
st
constructor for Foo
Foo(int n) { … }
nd
constructor for Foo
Package
Group related classes under one name
Separate namespace for each package
Package name added in front of actual name
Put generic / utility classes in packages
Avoid code duplication
package edu.umd.cs;
// name of package