Object-Oriented Programming II: Java Support for OOP - Prof. Nelson Padua-Perez, Study notes of Computer Science

Material Type: Notes; Professor: Padua-Perez; Class: OBJECT-ORIENTED PROG II; Subject: Computer Science; University: University of Maryland; Term: Spring 2007;

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-e86-1
koofers-user-e86-1 🇺🇸

10 documents

1 / 33

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 132:
Object-Oriented Programming II
Java Support for OOP
Department of Computer Science
University of Maryland, College Park
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21

Partial preview of the text

Download Object-Oriented Programming II: Java Support for OOP - Prof. Nelson Padua-Perez and more Study notes Computer Science in PDF only on Docsity!

CMSC 132:

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

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

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

“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

“super” Reference

Description

Reserved keyword Refers to superclass Allows object to refer to methods / variables insuperclass

Examples

super.x

// accesses variable x in superclass

super()

// invokes constructor in superclass

super.foo()

// invokes method foo() in superclass

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

Initialization Block

Definition

Block of code used to initialize static & instancevariables for class

Motivation

Enable complex initializations for static variables

Control flow Exceptions

Share code between multiple constructors forsame 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 – 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 variable declaration

{ 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 throughreference variables If no reference to object exists, object becomesgarbage (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

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( ) { … }

st

constructor for Foo

Foo(int n) { … }

nd

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