You are on page 1of 188

Sun Educational Services

Migrating to OO Programming
With Java Technology
SL-210

Migrating to OO Programming With Java Technology

December 2000

Sun Educational Services

Preface
About This Course

Migrating to OO Programming With Java Technology

December 2000

Sun Educational Services

Course Goal
This course provides the necessary concepts for developers
with a non-object-oriented background to begin the move to
Java technology.
This is not a Java technology programming course; it is a
preparatory course that provides the necessary knowledge for
taking SL-275: Java Programming Language.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Preface, slide 2 of 17

Sun Educational Services

Course Overview
Object-oriented analysis and design
Basic syntax for Java technology classes ("Java classes")
and structure of Java technology programs ("Java
programs")
Unified Modeling Language (UML) notation

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Preface, slide 3 of 17

Sun Educational Services

Course Expectations
This is not a programming course; no computers will be
used.
OO is a new paradigm and cannot be related to prior
non-OO programming experience.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Preface, slide 4 of 17

Sun Educational Services

Course Map

Migrating to OO Programming With Java Technology

Preface, slide 5 of 17

Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, December 2000, Revision A.3

Sun Educational Services

Module-by-Module Overview
Module 1 "Objects"
Module 2 "Classes"
Module 3 "Using Java Classes"
Module 4 "Using Java Methods"
Module 5 "Object Interaction"
Module 6 "Object-Oriented Analysis and Design
Using UML"

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Preface, slide 6 of 17

Sun Educational Services

Course Objectives
Identify objects in a problem domain
Group objects in classes
Identify objects relationships to one another
Use inheritance to create a specialized Java class
Describe polymorphism
Use encapsulation when creating Java classes

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Preface, slide 7 of 17

Sun Educational Services

Course Objectives
Define methods for a class
Describe basic Java technology programming structure
guidelines
Implement the understanding gained through OO and
Java technology syntax by developing UML diagrams
to analyze a problem domain

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Preface, slide 8 of 17

Sun Educational Services

Skills Gained by Module


Meaning of:
Black boxes
Gray boxes

Module
Skills Gained

Skill or Objective 1
Skill or Objective 2
Skill or Objective 3
Skill or Objective 4

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Preface, slide 9 of 17

Sun Educational Services

Guidelines for Module Pacing


Module

Day 1

About This Course

A.M.

Objects

A.M.

Classes

A.M./P.M.

Using Java Classes

P.M.

Day 2

Using Java Methods

A.M.

Object Interaction

A.M./P.M.

Object-Oriented Analysis and Design


Using UML

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Day 3

A.M./P.M.

Preface, slide 10 of 17

Sun Educational Services

Topics Not Covered


Advanced Java technology programming Covered in
SL-275: Java Programming Language
Advanced OO concepts Covered in OO-226: ObjectOriented Application Analysis and Design for Java
Technology (UML)

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Preface, slide 11 of 17

Sun Educational Services

How Prepared Are You?


Six months or more of experience programming
Experience designing applications

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Preface, slide 12 of 17

Sun Educational Services

Introductions
Name
Company affiliation
Title, function, and job responsibility
Programming experience
Reasons for enrolling in this course
Expectations for this course

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Preface, slide 13 of 17

Sun Educational Services

How to Use Course Materials


Relevance
Overhead image
Lecture
Exercise
Check Your Progress
Think Beyond
Acronym glossary

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Preface, slide 14 of 17

Sun Educational Services

Icons
Demonstration

Reference

Discussion
Exercise

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Preface, slide 15 of 17

Sun Educational Services

Typographical Conventions
Typeface or
Symbol

Meaning

Example

AaBbCc123

The names of commands, files,


and directories;
on-screen computer output

Edit your .login file.


Use ls -a to list all files.
system% You have mail.

AaBbCc123

What you type, contrasted with system% su


on-screen computer output
Password:

AaBbCc123

Command-line placeholder
replace with a real value

To delete a file, type rm


filename.

AaBbCc123

Book titles, new words or


terms, or words to be
emphasized

Read Chapter 6 in Users


Guide. These are called class
options.
You must be root to do this.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Preface, slide 16 of 17

Sun Educational Services

Syntax Conventions
Syntax and example
type variable_identifier
int myFirstVariable;

Optional elements in square brackets


[class_modifier] class name {block}

Variables in italics

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Preface, slide 17 of 17

Sun Educational Services

Module 1
Objects

Migrating to OO Programming With Java Technology

December 2000

Sun Educational Services

Objectives
Describe abstraction and how it is used in object
orientation
Identify objects and non-objects from a problem domain
Describe object encapsulation

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 1, slide 2 of 26

Sun Educational Services

Overview of Object Orientation


Technique for system modeling
Models the system as a number of related objects that
interact
Similar to the way people view their environment

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 1, slide 3 of 26

Sun Educational Services

Abstraction
Is the process of ignoring details to concentrate on
essential characteristics
Is the primary means of coping with complexity
Simplifies users interaction with abstracted objects
Has two types: functional abstraction and data
abstraction

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 1, slide 4 of 26

Sun Educational Services

Functional Abstraction
for (; *s== ' ' || *s == '\t'; s++);
sign=1;
if (*s =='+' || *s == '-')
sign = (*s++ == '+') ? 1: -1;
for (n=0; *s > '0' && *s <= '0'; s++)
n = 10 * n + *s - '0';
value = sign * n;

Unabstracted function

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 1, slide 5 of 26

Sun Educational Services

Data Abstraction
Deals with data types
Allows details of the internal representation of a data
type to be ignored
Involves functional abstraction

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 1, slide 6 of 26

Sun Educational Services

Identifying Objects
Object can be a sentence, bank account, number, or car
Objects are
Things
Real or imaginary
Simple or complex

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 1, slide 7 of 26

Sun Educational Services

Identifying Objects

Satellite dish

Employee

Painters palette

X + Yi
Complex number

Bank account

Bicycle

Example objects

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 1, slide 8 of 26

Sun Educational Services

Case Study
DirectClothing, Inc. nouns

catalog

clothing

subscribers

closeout items

monthly sale items

normal items

order

customer

CSR (customer service


representative)

order entry clerk

supplier

payment

warehouse

credit card

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

order entry
mail order
fax order
online order
inventory
back-ordered
items
system
Internet
business
year
month
order form
check

Module 1, slide 9 of 26

Sun Educational Services

Identifying Object Attributes and


Operations
Example:
Cloud attributes size, water content, shape
Cloud operations rain, thunder, snow
Attributes: an objects characteristics
Operations: what an object can do

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 1, slide 10 of 26

Sun Educational Services

Case Study
For the Order object, the following attributes and
operations could be defined:
Attributes: orderNumber, customerNumber,
dateOrdered, amountOwed
Operations: whatCustomer, calcAmountOwed,
printOrder, payOrder
Discussion What would be the attributes and
operations for the Customer object?

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 1, slide 11 of 26

Sun Educational Services

Testing an Identified Object


Use the following criteria to test object validity:
Relevance to the problem domain
Need to exist independently
Having attributes and operations

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 1, slide 12 of 26

Sun Educational Services

Relevance to the Problem Domain


Does it exist within the boundaries of the problem
statement?
Is it required in order for the system to fulfill its
responsibility?
Is it required as part of an interaction between a user
and the system?
Can objects sometimes be a characteristic of other
objects?

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 1, slide 13 of 26

Sun Educational Services

Case Study
The Order object passes the relevance test:
Exists within the boundaries of the problem
statement
Is required for the system to fulfill its responsibilities
Is required as part of an interaction between a user
and the system
Discussion Test the other candidate objects in the case
study.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 1, slide 14 of 26

Sun Educational Services

Independent Existence
To be an object and not a characteristic of another object,
the object must need to exist independently.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 1, slide 15 of 26

Sun Educational Services

Case Study
Can an Order object exist without any of the other
objects? It can, but it must have an associated Customer
object.
Address could be an attribute of Customer, but in this
case study it is advantageous for Address to be a
separate object.
Discussion Are there any other objects in the case
study that need to exist independently?

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 1, slide 16 of 26

Sun Educational Services

Attributes and Operations


An object must have attributes and operations.
If it does not, it is probably an attribute or operation of
another object.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 1, slide 17 of 26

Sun Educational Services

Case Study
The Order object has many attributes and operations
defined as do most of the candidate objects.
Are there any objects listed on page 1-10 that do not
have attributes and operations?

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 1, slide 18 of 26

Sun Educational Services

Encapsulation
Encapsulation separates the external aspects of an object
from the internal implementation details.
Internal changes need not affect external interface.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 1, slide 19 of 26

Sun Educational Services

Parts of an Encapsulated Object


Each physical object has
Public interface (external)
Implementation (internal)
Internal information (information for implementation)

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 1, slide 20 of 26

Sun Educational Services

Implementing Encapsulation
An objects attributes and operations are its members.
The members of an object can be public or private.
In pure OO systems, all attributes are private and can be
changed or accessed only through public operations.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 1, slide 21 of 26

Sun Educational Services

Case Study
Using the Order object from the case study, you would
make all attributes private and need to create public
operations to get and set values for each of the
attributes.
All other operations, such as calcAmountOwed and
payOrder, would also be public.
However, you could decide that the calcAmountOwed
should be private so other objects cannot have access to
calculate the amount owed.
Discussion How would you encapsulate the Customer
object?

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 1, slide 22 of 26

Sun Educational Services

Module Summary
Object
Attributes
Operations
Abstraction
Data hiding
Encapsulate
Member

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 1, slide 23 of 26

Sun Educational Services

Exercise: Identifying Objects


Exercise objective
Preparation
Tasks
Exercise summary

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 1, slide 24 of 26

Sun Educational Services

Check Your Progress


Describe abstraction and how it is used in object
orientation
Identify objects and non-objects from a problem domain
Describe object encapsulation

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 1, slide 25 of 26

Sun Educational Services

Think Beyond
What if you have several objects in a system that share
many characteristics but are different, such as different
types of musical instruments?
How would you write them or categorize them to
promote reuse?

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 1, slide 26 of 26

Sun Educational Services

Module 2
Classes

Migrating to OO Programming With Java Technology

December 2000

Sun Educational Services

Objectives
Group objects with similar attributes and common
operations in classes
Explain how classes are used to define objects
Define inheritance and explain how it relates to
software reuse
Define generalization and specialization and how they
relate to inheritance
Define polymorphism and explain how inheritance
promotes polymorphism
Define abstract classes
Migrating to OO Programming With Java Technology
Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 2, slide 2 of 22

Sun Educational Services

Class Overview

Objects of
a Leaf class

Objects of a
Ball class

Object is an instance: a specific leaf, ball, car, or coin


"A car" is a class; "my car" is an object

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 2, slide 3 of 22

Sun Educational Services

Case Study
For each object identified in the order entry system, define a
corresponding class.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 2, slide 4 of 22

Sun Educational Services

Generalization
Example: Transport is a generalization of several classes
that provide transportation.
Generalization identifies and defines the common
attributes and operations in a collection of objects.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 2, slide 5 of 22

Sun Educational Services

Case Study
The order entry clerk and customer service
representative objects are considered identical and an
Employee class will be created. You can create a
CustomerServiceRepresentative or an OrderEntryClerk
object from the Employee class.
The Customer object and the Employee class also have
similarities. You could create a Person class that would
contain those similar attributes; you could also create an
Address class.
Discussion What other generalizations can you find in
the objects?

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 2, slide 6 of 22

Sun Educational Services

Inheritance
Example: Soccer Ball class can inherit members from a Ball
class.
Superclass

Subclasses

Basketball

Ball

Soccer Ball

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Baseball

Module 2, slide 7 of 22

Sun Educational Services

Inheritance
Is a mechanism for defining a new class in terms of an
existing class
Allows you to group related classes so that they can be
managed collectively
Promotes reuse
Allows you to hide or override inherited members
Relevant terms: generalization, specialization, override

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 2, slide 8 of 22

Sun Educational Services

Case Study
Both the Customer and Employee classes
extend Person.
attributes
ID
firstName
lastName
initial
methods
getName

Person

Customer
attributes
ID
firstName
lastName
initial
*salutation
methods
getName
getSalutation

attributes
ID
firstName
lastName
initial
*SSN
methods
getName
getSSN
grantRaise

Employee

OE Clerk

CS Rep.

Discussion What other classes could you


use in this system to promote reuse?

Migrating to OO Programming With Java Technology

Module 2, slide 9 of 22

Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, December 2000, Revision A.3

Sun Educational Services

Specialization
Generalized
class:

Classes:

Automobile

Transport

Bicycle

Plane

Commercial

Specialized
classes:
Car

Truck

Mountain

Road

Private

Specialization is inheritance with the addition and


modification of methods to solve a specific problem.
Migrating to OO Programming With Java Technology
Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 2, slide 10 of 22

Sun Educational Services

Polymorphism
Allows you to implement an inherited operation in a
subclass
Works only when the common operation gives the same
semantic result
Implementation of a polymorphic function depends on
the object it is applied to
Can be used only with inheritance

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 2, slide 11 of 22

Sun Educational Services

Another Example
Consider a general class called WritingInstrument
The class has an operation called write
What are examples of objects in the classroom that
could be subclasses of WritingInstrument?
How is the write operation polymorphic?

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 2, slide 12 of 22

Sun Educational Services

Case Study
In the order-entry system, you could use polymorphism
with the Person objects giveName operation. Use it as
is for the Employee object, but all Customers would
give the salutation that should be used with their name,
in addition.
Discussion What other operations could be
polymorphic?

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 2, slide 13 of 22

Sun Educational Services

Key Points of Polymorphism


Based on inheritance
Can be applied to any operation that is inherited from a
superclass
Subclasses that override an inherited operation are
providing another form of the operation, hence the term
polymorphism

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 2, slide 14 of 22

Sun Educational Services

Abstract Classes
A class with at least one abstract operation is an abstract
class.
A subclass of an abstract class needs to provide an
implementation of all abstract operations or it is also an
abstract class.
An abstract class cannot be instantiated (you cannot
create objects of an abstract class).
Operations in an abstract class are placeholders.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 2, slide 15 of 22

Sun Educational Services

Another Example
Consider implementing classes for a circle, rectangle,
and triangle.
Operations for these classes must include setColor and
setBorderStyle.
You decide to create an abstract class called share with
two abstract operations: setColor and setBorderStyle
The subclasses must override these abstract operations
and provide implementation code that makes sense for
the subclass shape.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 2, slide 16 of 22

Sun Educational Services

Case Study
The Person object in the order-entry system is an
abstract class; you would instantiate a customer or an
employee, but not a person.
Discussion What other abstract classes could you
use in an order-entry system?

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 2, slide 17 of 22

Sun Educational Services

Key Points of Abstract Classes


A class with at least one abstract operation (an operation
for which there is no implementation code defined) is
an abstract class.
Subclasses of an abstract class should provide an
implementation of all inherited abstract operations. If
not, the subclasses are abstract classes.
Abstract classes are referred to as templates for classes.
No instance of an abstract class can be created.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 2, slide 18 of 22

Sun Educational Services

Module Summary
Class
Object
Generalization
Inheritance
Overriding
Specialization
Polymorphism
Abstract class

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 2, slide 19 of 22

Sun Educational Services

Exercise: Grouping Objects in Classes


Exercise objective
Preparation
Tasks
Exercise summary

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 2, slide 20 of 22

Sun Educational Services

Check Your Progress


Group objects with similar attributes and common
operations in classes
Explain how classes are used to define objects
Define inheritance and explain how it relates to
software reuse
Define generalization and specialization and how they
relate to inheritance
Define polymorphism and explain how inheritance
promotes polymorphism
Define abstract classes
Migrating to OO Programming With Java Technology
Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 2, slide 21 of 22

Sun Educational Services

Think Beyond
What components would need to be specified in a Java
programming language statement to describe a class?

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 2, slide 22 of 22

Sun Educational Services

Module 3
Using Java Classes

Migrating to OO Programming With Java Technology

December 2000

Sun Educational Services

Objectives
Code class declarations
Define the primitive types
Declare class variables using primitive types
Declare class variables using reference types
Name Java classes and other identifiers according to
Java programming language guidelines
Use inheritance correctly
Use abstract classes

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 2 of 46

Sun Educational Services

Objectives
Use the import statement to include classes in a
program
Use the package statement to group classes in a class
hierarchy
Understand the structure of a Java program

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 3 of 46

Sun Educational Services

Declaring Classes
[class_modifier] class class_identifier
{block}

class_modifier optional; public, abstract, or


final
class keyword indicating class declaration
class_identifier name of the class
block for variables and methods that make up the class

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 4 of 46

Sun Educational Services

Case Study
Code an Order class for the order-entry program like
this:
public class Order { }

Discussion How would you code the class declaration


for the other classes?

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 5 of 46

Sun Educational Services

Variables
Variable is the Java programming language term for
attributes.
Each must have type and unique identifier.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 6 of 46

Sun Educational Services

Identifiers
Identifiers are names assigned to classes, variables, and
methods.
The first character must be one of the following:
An uppercase letter (AZ)
A lowercase letter (az)
The underscore character (_)
The dollar character ($)

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 7 of 46

Sun Educational Services

Identifiers
The second and subsequent characters of an identifier
must be any of the following:
Any character from previous list
Numeric characters (09)
In multiple-word identifiers, start each subsequent
word with a capital letter.
Java keywords may not be used.
The Java programming language is case sensitive.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 8 of 46

Sun Educational Services

Java Primitive Types and Reference


Types
All variables have a type from a Java technology
primitive type or reference type.
Type restricts values that can be assigned to a variable.
Variable declaration syntax is
type variable_identifier

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 9 of 46

Sun Educational Services

Java Primitive Types and Reference


Types
Primitive types

byte
short
int
long
float
double
char
boolean

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 10 of 46

Sun Educational Services

Integral Types
Is used to store whole numbers
Counts zero as positive
Name or Type

Integer Length

Range

byte

8 bits

-27 ... 27 -1

short

16 bits

-215 ... 215 -1

int

32 bits

-231 ... 231 -1

long

64 bits

-263 ... 263 -1

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 11 of 46

Sun Educational Services

Floating Point Types


Floating point types store numbers with values to the
right of the decimal point.
Name or Type
float
double

Float Length
32 bits
64 bits

Largest or smallest value cannot be known because


floating point types allow variable accuracy.
Two pre-defined constants denote the extents of the
types, POSITIVE_INFINITY and NEGATIVE_INFINITY.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 12 of 46

Sun Educational Services

Textual Type: char


Stores a single character
Uses single quotation marks around value:
myFirstCharacter = g;

Can store any value from the Java technology character


set (Unicode)

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 13 of 46

Sun Educational Services

Logical Type: boolean


Stores one of two values: true or false
Is used in decision-making

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 14 of 46

Sun Educational Services

Object References
Hold a handle of an object
Can only refer to objects of the declared type or of a
subclass type

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 15 of 46

Sun Educational Services

Object References
Declaring an object reference:
type variable_identifier
Computer laptop;

Creating objects:
variable_identifier = new class_name
laptop = new Computer();

Using object references


int first = 9;
int second = first;
Computer laptop = new Computer();
Computer anotherReferenceToLaptop = laptop;

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 16 of 46

Sun Educational Services

Strings
Reference type used to store a sequence of characters
Creating Strings:
Method 1
String variable_identifier = new String
"string_value");
String animal = new String("dog");

Method 2
String variable = "string_value";
String animal = "dog";
Migrating to OO Programming With Java Technology
Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 17 of 46

Sun Educational Services

Case Study
The Person class declaration with the variables defined
would appears as shown:
1
2
3
4
5
6
7
8

class Person
{
String firstName;
String lastName;
String initial;
String ID;
Address homeAddress
}

How would you code the class declaration and


variables for the Order class?

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 18 of 46

Sun Educational Services

Encapsulation
Encapsulation hides implementation from users
All variables are should be private (hidden in
implementation)
Private variables can be accessed only through public
methods (operations)

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 19 of 46

Sun Educational Services

Case Study
The following class demonstrates making all variables private
and making the operations public:
1 class Person
2 {
3
private String firstName;
4
private String lastName;
5
private String initial;
6
private int ID;
7
private Address homeAddress;
8
9
public String getName(){return firstName;}
10
public void setFirstName(String fName){}
11
public void setLastName(String lName){}
12
public void setInitial(String init){}
13
public int getID(){return ID;}
14
public void setID(Sting id){}
15
public Address getAddress(){return homeAddress;}
16
public void setAddress(Address addr){}
17 }
Migrating to OO Programming With Java Technology
Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 20 of 46

Sun Educational Services

Inheritance
Example: Manager and Engineer classes both have
characteristics of an Employee.
Common items are defined in one class and subsequent
classes base themselves on that class.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 21 of 46

Sun Educational Services

Using Inheritance
Declaration of Manager and Engineer
classes:
1 class Manager
2 {
3
int employeeNumber;
4
String name;
5
int departmentNumber;
6
int extensionNumber;
7
int salary;
8
int numberOfWorkers;
9
// and so on
10 }

1 class Engineer
2 {
3
int employeeNumber;
4
String name;
5
int departmentNumber;
6
int extensionNumber;
7
int salary;
8
Manager worksFor;
9
// and so on
10 }

Migrating to OO Programming With Java Technology

Module 3, slide 22 of 46

Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, December 2000, Revision A.3

Sun Educational Services

Using Inheritance
Syntax for inheritance:
class subclass_name extends superclass_name
{block}

Example:
1
2
3
4
5
6
7
8
9

class Employee
{
int employeeNumber;
String name;
int departmentNumber;
int extensionNumber;
int salary;
// and so on
}

1
2
3
4
5
6
7
8
9
10
11

class Manager extends Employee


{
int numberOfWorkers;
// and so on
}
class Engineer extends Employee
{
Manager worksFor;
// and so on
}

Migrating to OO Programming With Java Technology

Module 3, slide 23 of 46

Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, December 2000, Revision A.3

Sun Educational Services

Testing Inheritance
In the Java programming language, a class can inherit
from only one superclass at a time.
It is very important to use inheritance only if it is valid
or unavoidable.
Check validity using the is a phrase (a Manager is an
Employee).

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 24 of 46

Sun Educational Services

Testing Inheritance
Class declaration with duplication:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

class Cycle
{
int numberOfWheels;
int numberOfSeats;
float luggageCapacity;
// and so on
}
class Boeing747
{
int numberOfWheels;
int numberOfSeats;
float luggageCapacity;
int numberOfWings;
// and so on
}

First attempt to use inheritance:


1
2
3
4
5
6
7
8
9
10
11
12

class Cycle
{
int numberOfWheels;
int numberOfSeats;
float luggageCapacity;
// and so on
}
class Boeing747 extends Cycle
{
int numberOfWings;
// and so on
}

Migrating to OO Programming With Java Technology

Module 3, slide 25 of 46

Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, December 2000, Revision A.3

Sun Educational Services

Testing Inheritance
Correct use of inheritance:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

class Vehicle
{
int numberOFWheels;
int numberOfSeats;
float luggageCapacity;
// and so on
}
class Cycle extends Vehicle
{
// and so on
}
class Boeing747 extends Vehicle
{
int numberOfWings;
// and so on
}

Migrating to OO Programming With Java Technology

Module 3, slide 26 of 46

Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, December 2000, Revision A.3

Sun Educational Services

Case Study
Remember the example of the Customer and Employee
classes; you could code the following class declarations
for inheritance:
class Customer extends Person {}
class Employee extends Person {}

Discussion What other classes would use inheritance?

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 27 of 46

Sun Educational Services

Containment Classes
Example: Kitchen class can be used to contain Stove
and Refrigerator classes
1 class Stove
2 {
3
//whatever the class does
4 }
5
6 class Refrigerator
7 {
8
// whatever the class does
9 }
10
11class Kitchen
12{
13 Stove myStove;
14 Refrigerator myRefrigerator;
15 // and so on
16}

Used to contain, or collect, references to related objects


in order to manipulate them
Migrating to OO Programming With Java Technology
Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 28 of 46

Sun Educational Services

Testing Containment
Use the "has a" phrase to test containment validity (a
Kitchen has a Stove; a Kitchen has a Refrigerator)

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 29 of 46

Sun Educational Services

Case Study
The Person class is a containment
class because it contains the home
address of the person.
1 class Person
2 {
3
private String firstName;
4
private String lastName;
5
private String initial;
6
private int ID;
7
private Address homeAddress;
8
9
public String getName(){return firstName;}
10
public void setFirstName(String fName){}
11
public void setLastName(String lName){}
12
public void setInitial(String init){}
13
public int getID(){return ID;}
14
public void setID(Sting id){}
15
public Address getAddress(){return homeAddress;}
16
public void setAddress(Address addr){}
17 }

Which other classes could be


containment classes?

Migrating to OO Programming With Java Technology

Module 3, slide 30 of 46

Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, December 2000, Revision A.3

Sun Educational Services

Abstract Classes and Inheritance


Example: A Drawing class contains methods for a
variety of drawing features, implemented in a platformindependent way.
Abstract classes can define the methods that should
exist; the methods will be implemented in subclasses.
Abstract classes and operations are marked as
abstract.
You cannot create an instance of an abstract class.
Subclasses must provide implementations for all
abstract methods in their superclasses.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 31 of 46

Sun Educational Services

Using Abstract Classes


1 public abstract class Drawing
2 {
3
4
public abstract void drawDot(int x, int y);
5
public void drawLine(int x1, int y1,
6
int x2, int y2)
7
{
8
// draw using the drawDot() method repeatedly.
9
}
10 }

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 32 of 46

Sun Educational Services

Case Study
Code an abstract class Person:
1 public abstract class Person
2 {
3
private String firstName;
4
private String lastName;
5
private String initial;
6
private int ID;
7
private Address homeAddress;
8
9
public String getName(){return firstName;}
10
public void setFirstName(String fName){}
11
public void setLastName(String lName){}
12
public void setInitial(String init){}
13
public int getID(){return ID;}
14
public void setID(Sting id){}
15
public Address getAddress(){return homeAddress;}
16
public void setAddress(Address addr){}
17 }

How would you implement other abstract classes?


Migrating to OO Programming With Java Technology
Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 33 of 46

Sun Educational Services

Classes and Packages


A package is a grouping of related classes.
You can
Import classes from a package
Package classes in your programs

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 34 of 46

Sun Educational Services

Java 2 Platform Class Library Packages


Java 2 platform comes with a standard set of classes that
implement most basic operations.
java.lang contains core classes.
java.awt deals with the graphical user interface.
java.applet provides applet-specific behavior.
java.net performs network related operations.
java.io contains classes that deal with file I/O.
java.util contains utility classes.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 35 of 46

Sun Educational Services

Grouping Classes in Packages


Packages can be used when distributing an application.
Indicate that classes in a source file belong to a
particular package by using the package statement:
package package_name
1
2
3
4
5
6
7
8

// Class Employee of the Finance department for the


// ABC company
package abc.financedept;
public class Employee
{
...
}

All classes in the source file are within the named


package.
Migrating to OO Programming With Java Technology
Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 36 of 46

Sun Educational Services

Case Study
Discussion What parts of the order-entry system would you
package? Code the package declaration.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 37 of 46

Sun Educational Services

Using Classes Stored in Packages


To use packages, use the import statement at the top of
your program:
import package_name

Example: Use the import statement and just the class


name Employee to access the Employee class in the
abc.financeDept package:
1
2
3
4
5
6
7

// Class Manager belongs to the default package.


import abc.financeDept.*;
public class Manager extends Employee
{
String department;
Employee [] subordinates;
}

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 38 of 46

Sun Educational Services

Coding Structure
Java is a free-format programming language with few
rules:
Source file can contain three elements, in this order:
package declaration (optional), import statements,
and class and interface definitions.
Source file name must match the public class name in
the source file.
You can have only one public class in a source file.
The Java source file is always named
public_class_name.java; the compiled version is
one or more files named class_name.class.
Migrating to OO Programming With Java Technology
Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 39 of 46

Sun Educational Services

Case Study
The following code example shows how to add the class
Customer to the package OrderEntry and import the Person
and Address classes from the OrderSupport package:
1
2
3

package OrderEntry;
import OrderSupport.Person;
import OrderSupport.Address;

5 public class Customer extends Person


6 {
7
private Address shipTo;
8
9
public Address getShipTo(){}
10
public void setShipTo(Address addr){}
11 }

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 40 of 46

Sun Educational Services

Module Summary
1. Defines a block that contains all your attributes and
operations.
2. Access modifier that gives the entire world access.
3. Modifier that restricts the use of subclassing.
4. Primitive data type that may have the values true or
false.
5. Primitive data type that holds textual information.
6. Primitive data type that whole numbers default to.
7. Primitive data type that is most often used for
decimal numbers.
Migrating to OO Programming With Java Technology
Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 41 of 46

Sun Educational Services

8. Keyword that allocates space and initializes member


variables.
9. Class from java.lang that holds textual information.
10.Keyword that allows you to do subclassing.
11.Keyword that defines a class that cannot be created.
12.Keyword that defines this code as part of a library.
13.Keyword that loads code from a library.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 42 of 46

Sun Educational Services

Exercise: Implementing the Class


Diagram
Exercise objective
Preparation
Tasks
Exercise summary

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 43 of 46

Sun Educational Services

Check Your Progress


Code class declarations
Define the primitive types
Declare class variables using primitive types
Declare class variables using reference types
Name Java classes and other identifiers according to
Java programming language guidelines
Use inheritance correctly
Use abstract classes

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 44 of 46

Sun Educational Services

Check Your Progress


Use the import statement to include classes in a
program
Use the package statement to group classes in a class
hierarchy
Understand the structure of a Java program

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 45 of 46

Sun Educational Services

Think Beyond
Now that you have written a class in the Java programming
language, how do you write an operation (method)? What
information would you need to provide?

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 3, slide 46 of 46

Sun Educational Services

Module 4
Using Java Methods

Migrating to OO Programming With Java Technology

December 2000

Sun Educational Services

Objectives
Define class behavior using methods
Declare a public method for encapsulation
Pass arguments to a method
Describe a methods signature
Explain how method overloading works
Use a constructor to instantiate an object

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 4, slide 2 of 31

Sun Educational Services

Method Overview
Method is the Java technology term for an OO operation.
Methods separate the statements into code blocks that
can run independently of each other.
Methods can invoke each other.
All programs use methods.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 4, slide 3 of 31

Sun Educational Services

Why Are Methods Necessary?


Methods make programs more readable and easier to
maintain.
Methods make development and maintenance quicker.
Methods are central to reusable software.
Methods avoid duplication.
Methods are required for encapsulation.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 4, slide 4 of 31

Sun Educational Services

Declaring Methods
Methods are written inside the body of a class.

modifiers return_type method_identifier


([arguments]) {method_body}
modifiers Keywords that modify the way
methods are stored or how they run
return_type Result of calculation or query
method_identifier Identifier (name)
arguments (Optional) Data for the method
method_body Sequence of statements to perform
the task
Migrating to OO Programming With Java Technology
Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 4, slide 5 of 31

Sun Educational Services

Case Study
The Person class attributes and
method declarations:
1 public abstract class Person
2 {
3
private String firstName;
4
private String lastName;
5
private String initial;
6
private int ID;
7
private Address homeAddress;
8
9
public String getName(){return firstName;}
10
public void setFirstName(String fName){}
11
public void setLastName(String lName){}
12
public void setInitial(String init){}
13
public int getID(){return ID;}
14
public void setID(String id){}
15
public Address getAddress(){return homeAddress;}
16
public void setAddress(Address addr){}
17 }

Discussion How would you code the


method declarations for the other
objects?

Migrating to OO Programming With Java Technology

Module 4, slide 6 of 31

Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, December 2000, Revision A.3

Sun Educational Services

Invoking Methods
object_reference.method_identifier([arguments])
1 class VoidMethodInvocation
2 {
3
public static void main (String args[])
4
{
5
ClassOne one = new ClassOne();
6
one.returnNothing();
7
}
8 }
9
10class ClassOne
11{
12 public void returnNothing()
13 {
14
//whatever the method does
15 }
16}

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 4, slide 7 of 31

Sun Educational Services

Case Study
Customer.java source file:
1
2
3
4
5
6
7
8
9
10

package OrderEntry;
import OrderSupport.Address;
public class Customer extends Person
{
public static void main (String args[])
{
Adress shipTo = new Address();
shipTo.printLabel();
}
}

Address.java source file:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

package OrderSupport;
public class Address
{
private String addressLine1;
private String addressLine2;
private String city;
private String state;
private String postalCode;
private String country;
public void printLabel()
{
System.out.println(addressLine1);
if (addressLine2 != null)
System.out.println(addressLine2);
System.out.println(city + ", "+ state +
postalCode + country);
}
}

Migrating to OO Programming With Java Technology

Module 4, slide 8 of 31

Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, December 2000, Revision A.3

Sun Educational Services

Object Methods
Method must be preceded by the reference to the object
and the dot operator (.).
1
2
3
4
5
6
7
8

class MethodInvocation
{
public static void main (String args[])
{
ClassOne one = new ClassOne();
int i = one.returnAnInt();
int j = i * one.returnAnInt() + 6;
System.out.println("An int: " +
one.returnAnInt());
9
}
10 }
11
12 class ClassOne
13 {
14
public int returnAnInt()
15
{
16
return 9;
17
}
18 }

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 4, slide 9 of 31

Sun Educational Services

static Methods
The static modifier should be used with generic
utilities or methods that may need to be called any time
without first instantiating an object from the class.
1
2
3
4
5
6
7
8

class StaticExample
{
public static void main (string args[])
{
double rand = Math.random();
System.out.println("A random number: " + rand);
}
}

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 4, slide 10 of 31

Sun Educational Services

Case Study
Discussion What static methods could you use in the orderentry example?

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 4, slide 11 of 31

Sun Educational Services

The this Reference


Means "reference to the same object"
Takes the place of an explicit object reference
Example: Line 5 refers to a method2 method for the
current class
1 class Example
2 {
3
void method1()
4
{
5
this.method2();
6
}
7
void method2()
8
{
9
//whatever method2 does
10 }
11}

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 4, slide 12 of 31

Sun Educational Services

Passing Arguments
You must give values for any methods with arguments
when invoking the method.
Arguments must be declared as a type-variable pair:

method_name (type variable_identifier)


myMethod (int myInt)

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 4, slide 13 of 31

Sun Educational Services

Passing Arguments
1 class StoreVal
2 {
3
int value;
4
// method declaration for a method with no
5
// return value and one int argument
6
void setValue(int val)
7
{
8
// the this keyword sets value to val
9
this.value = val;
10
}
11
//method declaration for a method getValuePlus
12
//with an int return value and one int argument
13
int getValuePlus(int userdefined)
14
{
15
// return keyword returns the expression to the
16
// int returnvalue of the calling method,
17
// getValuePlus
18
return (value + userdefined);
19
}
20 }

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 4, slide 14 of 31

Sun Educational Services

Case Study
When the Customer class is instantiated, then the
initialize method on line 8 could be called which
will invoke the setShipTo method on line 10, passing
the addr to set the shipTo attribute.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

package OrderEntry;
import OrderSupport.Person;
import OrderSupport.Address;
public class Customer extends Person {
private Address shipTo;
public void initialize(Address addr) {
homeAddress = addr;
setShipTo(addr);
}
public Address getShipTo(){}
public void setShipTo(Address addr){}
}

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 4, slide 15 of 31

Sun Educational Services

Case Study
The Person class is provided to remind you that the
attribute homeAddress is part of the Person class and is
thus part of the Customer class based on inheritance.
1 package OrderSupport;
2 public abstract class Person {
3
private String firstName;
4
private String lastName;
5
private String initial;
6
private int ID;
7
private Address homeAddress;
8
9
public String getName(){return firstName;}
10
public void setFirstName(String fName){}
11
public void setLastName(String lName){}
12
public void setInitial(String init){}
13
public int getID(){return ID;}
14
public void setID(String id){}
15
public Address getAddress(){return homeAddress;}
16
public void setAddress(Address addr){}
17 }

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 4, slide 16 of 31

Sun Educational Services

Method Overloading
Method overloading is two or more methods in the same
class with the same name but different arguments.
Combination of name and arguments is a methods
signature.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 4, slide 17 of 31

Sun Educational Services

Using Method
Overloading
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

class Example
{
public static void main (String args[])
{
StoreVal store = new StoreVal();
store.setValue(9);
int i = store.getValuePlus(2);
double d = store.getValuePlus(3.6);
}
}
class StoreVal
{
int value;
void setValue(int val)
{
value = val;
}
int getValuePlus(int more)
{
return (value + more);
}
double getValuePlus(double more)
{
return (value + more);
}
}

Migrating to OO Programming With Java Technology

Module 4, slide 18 of 31

Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, December 2000, Revision A.3

Sun Educational Services

Case Study
Discussion What methods would it be useful to overload in
the order-entry system?

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 4, slide 19 of 31

Sun Educational Services

Constructors
Constructors are methods that allow you to have more
control when you initialize objects.
Constructors conform to all the rules for methods,
except:
Constructors have no return type.
The name of a constructor is always the same as the
name of the class.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 4, slide 20 of 31

Sun Educational Services

Writing a Constructor
Include the constructor name without arguments
within the class statement:
class_declaration
{
constructor_name([arguments])
{
[variable_initialization_statements];
}
}
1 class Shirt
2 {
3
String type;
4
5
// Shirt() is the constructor
6
Shirt()
7
{
8
type = "Oxford Shirt";
9
}
10}

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 4, slide 21 of 31

Sun Educational Services

Using a Constructor
To use a constructor to instantiate an object, use the
following syntax:
class_name reference_variable = new
constructor([arguments])

Example: Uses constructor from previous example to


instantiate a reference type myShirt (reference to Shirt
object)
Shirt myShirt = new Shirt();

Example: Uses a different constructor, Shirt(String


differentType), which has one String argument
Shirt myShirt = new Shirt("Dress Shirt");
Migrating to OO Programming With Java Technology
Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 4, slide 22 of 31

Sun Educational Services

Overloading Constructors
Constructors can be overloaded like other methods.
Example:
Create a new Shirt object using the first constructor,
Shirt()
Shirt myShirt = new Shirt();
Create a new Shirt object with the type Dress
Shirt, using the second constructor, Shirt(String
differentType)
Shirt myShirt = new Shirt("Dress Shirt");

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 4, slide 23 of 31

Sun Educational Services

Overloading Constructors
1 class Shirt
2 {
3
String type;
4
5
// Shirt() is the first constructor
6
Shirt()
7
{
8
type = "Oxford Shirt";
9
}
10
11 // Shirt(String differentType) is another
// constructor
12 Shirt(String differentType)
13 {
14
type = differentType;
15 }
16}

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 4, slide 24 of 31

Sun Educational Services

The Default Constructor


All classes must have at least one constructor, called the
default constructor.
1 class Sweater
2 {
3
String name;
4
5
// the default constructor
6
public Sweater()
7
{
8
}
9 }

The compiler inserts the default constructor when you


do not provide one of your own.
If you do provide a constructor (whatever its signature)
the compiler will not provide the default one.
Migrating to OO Programming With Java Technology
Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 4, slide 25 of 31

Sun Educational Services

Case Study
The initialize method on line 8 was
originally created so that the shipTo address
is initialized when the Customer is
instantiated.
This can be done more easily by replacing
the initialize method with the
constructor on line 13.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

package OrderEntry;
import OrderSupport.Person;
import OrderSupport.Address;
public class Customer extends Person {
private Address shipTo;
// public void initialize(Address addr) {
// homeAddress = addr;
// setShipTo(addr);
// }
Customer() {
setShipTo(homeAddress);
}
public Address getShipTo() {}
public void setShipTo(Address addr){}
}

Migrating to OO Programming With Java Technology

Module 4, slide 26 of 31

Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, December 2000, Revision A.3

Sun Educational Services

Coding Structure
Methods are written in a series of statements; each
statement ends with a semicolon (;).
Code structure boundaries are marked with braces ({}).
Methods and variables for a class must be declared
within the class statement.
Whitespaces (spaces, tabs and linefeeds) do not affect a
program, so you can use them to organize the
statements.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 4, slide 27 of 31

Sun Educational Services

Module Summary
Method
Recursion
main
Arguments
this
Overloading
Constructor
static

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 4, slide 28 of 31

Sun Educational Services

Exercise: Develop Methods and


Constructors
Exercise objective
Preparation
Tasks
Exercise summary

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 4, slide 29 of 31

Sun Educational Services

Check Your Progress


Define class behavior using methods
Declare a public method for encapsulation
Pass arguments to a method
Describe a methods signature
Explain how method overloading works
Use a constructor to instantiate an object

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 4, slide 30 of 31

Sun Educational Services

Think Beyond
In order for you to write programs, objects need to interact
with each other. What types of interaction would objects need
to do?

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 4, slide 31 of 31

Sun Educational Services

Module 5
Object Interaction

Migrating to OO Programming With Java Technology

December 2000

Sun Educational Services

Objectives
Explain how objects interact with each other through
object messaging
Define association
Define composition
Decide whether a relationship between two objects
should be association or composition
Define the lifetime of an object with regard to
association and composition
Define the custody of an object with regard to
association and composition
Migrating to OO Programming With Java Technology
Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 5, slide 2 of 17

Sun Educational Services

Object Messaging
One object sends a message to another (the receiving
object).
The receiving object may send other messages, change
its attribute, or react in any other appropriate way.
Messaging is handled by operations in the public
interface of the receiving object.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 5, slide 3 of 17

Sun Educational Services

Case Study
Customer.java source file:
1
2
3
4
5
6
7
8
9
10

package OrderEntry;
import OrderSupport.Address;
public class Customer extends Person
{
public static void main (String args[])
{
Address shipTo = new Address();
shipTo.printLabel();
}
}

Address.java source file:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

package OrderSupport;
public class Address
{
private String addressLine1;
private String addressLine2;
private String city;
private String state;
private String postalCode;
private String country;
public void printLabel()
{
System.out.println(addressLine1);
if (addressLine2 != null)
System.out.println(addressLine2);
System.out.println(city + , + state +
postalCode + country);
}
}

Migrating to OO Programming With Java Technology

Module 5, slide 4 of 17

Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, December 2000, Revision A.3

Sun Educational Services

Association and Composition


Objects interact through one of two relationships:
association or composition.
Association: Two independent objects collaborate to
achieve some goal, like a person using a computer
("uses a" relationship).
Composition: One object contains another, like a
pencil that has a lead ("has a" relationship).
Use containment classes to set up both relationships.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 5, slide 5 of 17

Sun Educational Services

Case Study
Discussion In the order-entry system, which classes have an
association relationship and which have a composition
relationship?

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 5, slide 6 of 17

Sun Educational Services

Implementing Association and


Composition
Association:
1 class Kitchen
2 {
3
Stove myStove;
4
Refrigerator myRefrigerator;
5
6
public void setStove (Stove aStove)
7
{
8
myStove = aStove;
9
}
10
11
public void setRefrigerator (Refrigerator aRefrigerator)
12
{
13
myRefrigerator = aRefrigerator;
14
}
15 }

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 5, slide 7 of 17

Sun Educational Services

Implementing Association
and Composition
Composition:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

class Seat
{
//whatever the class does
}
class Wheel
{
// whatever the class does
}
class Frame
{
// whatever the class does
}
class Car
{
Seat mySeat = new Seat();
Wheel myWheel = new Wheel();
Frame myFrame = new Frame();
// and so on
}

Migrating to OO Programming With Java Technology

Module 5, slide 8 of 17

Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, December 2000, Revision A.3

Sun Educational Services

Choosing Association or Composition


Consider flexibility, management, and what is right for
the object.
If the objects ever need to exist independently, use
association.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 5, slide 9 of 17

Sun Educational Services

Contrasting Composition and


Association
Composition

Association

Implements the has a re- Implements the uses a


lationship
relationship
Management is simpler,
components are created
automatically

Objects must be created


Associations and custody
must be established

Inflexible

Flexible
Useful when an object has
interchangeable components or when an object already exists

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 5, slide 10 of 17

Sun Educational Services

Lifetime of an Object
Lifetime is the time between when it is created and
when it is destroyed.
Composed objects have the same lifetimes.
Associated objects have overlapping lifetimes.
Constant association: an object cannot exist or be
created unless another object is always associated
with it.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 5, slide 11 of 17

Sun Educational Services

Custody of an Object
Custody is concerned with the ownership of an object
and the subsequent responsibility to destroy the object.
Composition: All objects are implicitly in the custody of
the composite object.
Association: Requires that the objects lifetimes overlap.
The creating object has custody, but the custody can be
passed.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 5, slide 12 of 17

Sun Educational Services

Case Study
Discussion Who has custody in the Order-Customer
relationship? Who has custody in Customer-Address?

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 5, slide 13 of 17

Sun Educational Services

Module Summary
Message passing
"uses a"
"has a"
Object lifetime
Object custody
Part/whole relationship

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 5, slide 14 of 17

Sun Educational Services

Exercise: Modeling Object


Relationships
Exercise objective
Preparation
Tasks
Exercise summary

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 5, slide 15 of 17

Sun Educational Services

Check Your Progress


Explain how objects interact with each other through
object messaging
Define association
Define composition
Decide whether a relationship between two objects
should be association or composition
Define the lifetime of an object with regard to
association and composition
Define the custody of an object with regard to
association and composition
Migrating to OO Programming With Java Technology
Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 5, slide 16 of 17

Sun Educational Services

Think Beyond
How ready do you feel to begin writing a program,
based on your identification of objects, attributes and
operations?
What would you do to explain your analysis to others if
you are working on the same project with several other
developers?

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 5, slide 17 of 17

Sun Educational Services

Module 6
Object-Oriented Analysis and Design
Using UML

Migrating to OO Programming With Java Technology

December 2000

Sun Educational Services

Objectives
Create a set of use cases to describe a problem domain
Create a sequence diagram for a use case
Create a class diagram for a problem domain
Create an activity diagram for a use case
Code class declarations for the class diagram

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 6, slide 2 of 22

Sun Educational Services

Object-Oriented Analysis and Design


This module provides a simplified approach to objectoriented analysis and design (OOA/D).
Unified Modeling Language (UML) is used to notate the
design.
UML diagrams covered in this module:
Use case diagram
Sequence diagrams
Class diagrams
Activity diagrams

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 6, slide 3 of 22

Sun Educational Services

Use Case Diagrams


A use case diagram contains use cases, actors, and
relationship links.
A use case is an interaction of a user with the
application in order to achieve a desired result.
An actor is a role that a user plays when interfacing
with the application.
Relationship links between use cases are uses and
extends.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 6, slide 4 of 22

Sun Educational Services

Use Case Diagrams


Enter order
Customer

<<uses>>
CSR

Check levels

OEC
<<extends>>

Inventory
system

Out-of-Stock
Assemble order
<<extends>>

Warehouse

Special packaging
for fragile items

Legend
Use case

Link

Actor

Migrating to OO Programming With Java Technology

Module 6, slide 5 of 22

Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, December 2000, Revision A.3

Sun Educational Services

Use Case Diagram Conventions


Actors on the left represent real people. Actors on the
right represent processes or systems.
Use cases are read from top to bottom and left to right
(use this convention in the absence of arrows).
Each link from an actor to a use case indicates the actor
carries out the process identified by the use case.
The same actor can be involved in more than one use
case.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 6, slide 6 of 22

Sun Educational Services

Exercise 1: Writing a Use Case


Description
Exercise objective
Tasks
Exercise summary

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 6, slide 7 of 22

Sun Educational Services

Sequence Diagrams
Capture the operations of a single use case and show
how groups of objects collaborate on those operations
Exist for each use case
Contain objects, object lifelines, messages between
objects, conditions, iteration markers, activations, and
object deletions

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 6, slide 8 of 22

Sun Educational Services

Sequence Diagram
Order
Window

Order

Order
Line

Item

Payment

initialize()
*[for each orderline]
initialize()
inStock:= check()
[inStock]
add()
reorder:= reorder()

[not inStock]
hold()
verify()

Legend
Object

[...]
*

Message

Activation

Condition
Iteration marker
Return

Migrating to OO Programming With Java Technology

Deletion

Module 6, slide 9 of 22

Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, December 2000, Revision A.3

Sun Educational Services

Exercise 2: Creating a Sequence


Diagram
Exercise objective
Preparation
Tasks
Exercise summary

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 6, slide 10 of 22

Sun Educational Services

Class Diagram From the Conceptual


Perspective
The class diagram shows object relationships and
includes
Objects in the application
Relationships between those objects (associations
and subtypes)
Relationship multiplicity (one-to-one, and so on)
Attributes

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 6, slide 11 of 22

Sun Educational Services

Class Diagram From the


Conceptual Perspective
Customer

Order
* 1

dateReceived
isPrepaid: boolean
number: String

shipTo: Address
billTo: Address

Employee
0..1

manager
department
dependents
SSN

Address
* 2

addressLine1
addressLine2
city
state
postalCode
country
1
1

Person
ID
firstName
lastName
initial
homeAddr:
Address

Order Line
quantity: Integer
price: Money

Item
*

number: String
description: String
unitPrice: Money

Legend

Classes

0..1, 1, *

Relationships

Multiplicities

Migrating to OO Programming With Java Technology

Module 6, slide 12 of 22

Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, December 2000, Revision A.3

Sun Educational Services

Exercise 3: Creating a Class Diagram


From the Conceptual Perspective
Exercise objective
Tasks
Exercise summary

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 6, slide 13 of 22

Sun Educational Services

Activity Diagrams
Activity diagrams show objects connection with
workflow and how the operations work in parallel
processing.
There is one activity diagram for each use case.
Diagram includes
Activities
Activity triggers
Trigger guards
Activity synchronization bars

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 6, slide 14 of 22

Sun Educational Services

Activity Diagrams
Enter
Order

* [for each orderline]


Cancel
Order

[failed]

Verify
Payment

Check

[out of stock]

Item

Hold
Order

[in stock]
Assign
to Order

[succeeded]

[all items
assigned
and payments
verified]

Dispatch
Order

Legend

Activity
Trigger

[...]

Guard
Synchronization bar

Migrating to OO Programming With Java Technology

Module 6, slide 15 of 22

Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, December 2000, Revision A.3

Sun Educational Services

Exercise 4: Creating an Activity


Diagram
Exercise objective
Tasks
Exercise summary

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 6, slide 16 of 22

Sun Educational Services

Class Diagram From the Specification


Perspective
The specification perspective of the class diagram is
concerned with the interfaces of the application.
Responsibilities and operations are added.

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 6, slide 17 of 22

Sun Educational Services

Class Diagram From the


Specification Perspective
Customer

Order
* 1

dateReceived
isPrepaid: boolean
number: String
shipOrder()

shipTo: Address
billTo: Address
name
verifyCredit()

Employee
0..1

manager
department
dependents
SSN
promote()

Address
* 2

addressLine1
addressLine2
city
state
postalCode
country
1
1

Person
ID
firstName
lastName
initial
homeAddr:
Address

Order Line
quantity: Integer
price: Money

Item
*

number: String
description: String
unitPrice: Money

Legend

Classes

0..1, 1, *

Relationships

Multiplicities

Migrating to OO Programming With Java Technology

Module 6, slide 18 of 22

Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, December 2000, Revision A.3

Sun Educational Services

Module Summary
Use cases
Sequence diagrams
Activity diagrams
Class diagrams
Difference between the conceptual and specification
perspectives

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 6, slide 19 of 22

Sun Educational Services

Exercise 5: Creating a Class Diagram


From the Specification Perspective
Exercise objective
Tasks
Exercise summary

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 6, slide 20 of 22

Sun Educational Services

Check Your Progress


Create a set of use cases to describe a problem domain
Create a sequence diagram for a use case
Create a class diagram for a problem domain
Create an activity diagram for a use case
Code class declarations for the class diagram

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 6, slide 21 of 22

Sun Educational Services

Think Beyond
Now that you have the OO concepts and Java programming
language basics, you are ready to proceed with your training
in the Java programming language. What should your next
step be?

Migrating to OO Programming With Java Technology


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000, Revision A.3

Module 6, slide 22 of 22

Course Contents
About This Course .......................................................................................................Preface-1
Course Goal .................................................................................................................................. Preface-2
Course Overview ......................................................................................................................... Preface-3
Course Expectations .................................................................................................................... Preface-4
Course Map ................................................................................................................................... Preface-5
Module-by-Module Overview ................................................................................................... Preface-6
Course Objectives ......................................................................................................................... Preface-7
Skills Gained by Module ............................................................................................................. Preface-9
Guidelines for Module Pacing ................................................................................................. Preface-10
Topics Not Covered ................................................................................................................... Preface-11
How Prepared Are You? ........................................................................................................... Preface-12
Introductions .............................................................................................................................. Preface-13
How to Use Course Materials .................................................................................................. Preface-14
Icons ............................................................................................................................................. Preface-15
Typographical Conventions ..................................................................................................... Preface-16
Syntax Conventions ................................................................................................................... Preface-17

Objects .....................................................................................................................................1-1
Objectives ................................................................................................................................................. 1-2
Overview of Object Orientation ............................................................................................................ 1-3
Abstraction ............................................................................................................................................... 1-4
Functional Abstraction ........................................................................................................................... 1-5
Data Abstraction ..................................................................................................................................... 1-6
Identifying Objects .................................................................................................................................. 1-7
Case Study ................................................................................................................................................ 1-9
Identifying Object Attributes and Operations .................................................................................. 1-10

Implementing Java Security


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000 Revision A.3

Sun Educational Services


Case Study .............................................................................................................................................. 1-11
Testing an Identified Object ................................................................................................................ 1-12
Relevance to the Problem Domain ..................................................................................................... 1-13
Case Study .............................................................................................................................................. 1-14
Independent Existence ......................................................................................................................... 1-15
Case Study .............................................................................................................................................. 1-16
Attributes and Operations ................................................................................................................... 1-17
Encapsulation ........................................................................................................................................ 1-19
Parts of an Encapsulated Object ......................................................................................................... 1-20
Implementing Encapsulation .............................................................................................................. 1-21
Case Study .............................................................................................................................................. 1-22
Module Summary ................................................................................................................................. 1-23
Exercise: Identifying Objects ............................................................................................................... 1-24
Check Your Progress ............................................................................................................................ 1-25
Think Beyond ........................................................................................................................................ 1-26

Classes ......................................................................................................................................2-1
Objectives ................................................................................................................................................. 2-2
Class Overview ........................................................................................................................................ 2-3
Case Study ................................................................................................................................................ 2-4
Generalization ......................................................................................................................................... 2-5
Case Study ................................................................................................................................................ 2-6
Inheritance ................................................................................................................................................ 2-8
Case Study ................................................................................................................................................ 2-9
Specialization ......................................................................................................................................... 2-10
Polymorphism ....................................................................................................................................... 2-11
Another Example .................................................................................................................................. 2-12
Case Study .............................................................................................................................................. 2-13
Key Points of Polymorphism .............................................................................................................. 2-14
Abstract Classes ..................................................................................................................................... 2-15

Implementing Java Security


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000 Revision A.3

ii

Sun Educational Services


Another Example .................................................................................................................................. 2-16
Case Study .............................................................................................................................................. 2-17
Key Points of Abstract Classes ............................................................................................................ 2-18
Module Summary ................................................................................................................................. 2-19
Exercise: Grouping Objects in Classes ............................................................................................... 2-20
Check Your Progress ............................................................................................................................ 2-21
Think Beyond ........................................................................................................................................ 2-22

Using Java Classes .................................................................................................................3-1


Objectives ................................................................................................................................................. 3-2
Declaring Classes .................................................................................................................................... 3-4
Case Study ................................................................................................................................................ 3-5
Variables ................................................................................................................................................... 3-6
Identifiers ................................................................................................................................................. 3-7
Java Primitive Types and Reference Types ......................................................................................... 3-9
Integral Types ........................................................................................................................................ 3-11
Floating Point Types ............................................................................................................................. 3-12
Textual Type: char .........................................................3-13
Logical Type: boolean ......................................................3-14
Object References .................................................................................................................................. 3-15
Strings ..................................................................................................................................................... 3-17
Case Study .............................................................................................................................................. 3-18
Encapsulation ........................................................................................................................................ 3-19
Case Study .............................................................................................................................................. 3-20
Inheritance .............................................................................................................................................. 3-21
Using Inheritance .................................................................................................................................. 3-22
Testing Inheritance ............................................................................................................................... 3-24
Case Study .............................................................................................................................................. 3-27
Containment Classes ............................................................................................................................ 3-28
Testing Containment ............................................................................................................................ 3-29

Implementing Java Security


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000 Revision A.3

iii

Sun Educational Services


Case Study .............................................................................................................................................. 3-30
Abstract Classes and Inheritance ........................................................................................................ 3-31
Using Abstract Classes ......................................................................................................................... 3-32
Case Study .............................................................................................................................................. 3-33
Classes and Packages ........................................................................................................................... 3-34
Java 2 Platform Class Library Packages ............................................................................................. 3-35
Grouping Classes in Packages ............................................................................................................ 3-36
Case Study .............................................................................................................................................. 3-37
Using Classes Stored in Packages ....................................................................................................... 3-38
Coding Structure ................................................................................................................................... 3-39
Case Study .............................................................................................................................................. 3-40
Module Summary ................................................................................................................................. 3-41
Exercise: Implementing the Class Diagram ...................................................................................... 3-43
Check Your Progress ............................................................................................................................ 3-44
Think Beyond ........................................................................................................................................ 3-46

Using Java Methods ...............................................................................................................4-1


Objectives ................................................................................................................................................. 4-2
Method Overview ................................................................................................................................... 4-3
Why Are Methods Necessary? .............................................................................................................. 4-4
Declaring Methods .................................................................................................................................. 4-5
Case Study ................................................................................................................................................ 4-6
Invoking Methods ................................................................................................................................... 4-7
Case Study ................................................................................................................................................ 4-8
Object Methods ........................................................................................................................................ 4-9
static Methods .................................................................................................................................... 4-10
Case Study .............................................................................................................................................. 4-11
The this Reference ............................................................................................................................... 4-12
Passing Arguments ............................................................................................................................... 4-13
Case Study .............................................................................................................................................. 4-15

Implementing Java Security


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000 Revision A.3

iv

Sun Educational Services


Method Overloading ............................................................................................................................ 4-17
Using Method Overloading ................................................................................................................. 4-18
Case Study .............................................................................................................................................. 4-19
Constructors ........................................................................................................................................... 4-20
Writing a Constructor ........................................................................................................................... 4-21
Using a Constructor .............................................................................................................................. 4-22
Overloading Constructors ................................................................................................................... 4-23
The Default Constructor ...................................................................................................................... 4-25
Case Study .............................................................................................................................................. 4-26
Coding Structure ................................................................................................................................... 4-27
Module Summary ................................................................................................................................. 4-28
Exercise: Develop Methods and Constructors .................................................................................. 4-29
Check Your Progress ............................................................................................................................ 4-30
Think Beyond ........................................................................................................................................ 4-31

Object Interaction ..................................................................................................................5-1


Objectives ................................................................................................................................................. 5-2
Object Messaging .................................................................................................................................... 5-3
Case Study ................................................................................................................................................ 5-4
Association and Composition ............................................................................................................... 5-5
Case Study ................................................................................................................................................ 5-6
Implementing Association and Composition ..................................................................................... 5-7
Choosing Association or Composition ................................................................................................ 5-9
Contrasting Composition and Association ....................................................................................... 5-10
Lifetime of an Object ............................................................................................................................. 5-11
Custody of an Object ............................................................................................................................ 5-12
Case Study .............................................................................................................................................. 5-13

Implementing Java Security


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000 Revision A.3

Sun Educational Services


Module Summary ................................................................................................................................. 5-14
Exercise: Modeling Object Relationships .......................................................................................... 5-15
Check Your Progress ............................................................................................................................ 5-16
Think Beyond ........................................................................................................................................ 5-17

Object-Oriented Analysis and Design Using UML ..........................................................6-1


Objectives ................................................................................................................................................. 6-2
Object-Oriented Analysis and Design .................................................................................................. 6-3
Use Case Diagrams ................................................................................................................................. 6-4
Use Case Diagram Conventions ........................................................................................................... 6-6
Exercise 1: Writing a Use Case Description ........................................................................................ 6-7
Sequence Diagrams ................................................................................................................................. 6-8
Exercise 2: Creating a Sequence Diagram ......................................................................................... 6-10
Class Diagram From the Conceptual Perspective ............................................................................ 6-11
Exercise 3: Creating a Class Diagram From the Conceptual Perspective ..................................... 6-13
Activity Diagrams ................................................................................................................................. 6-14
Exercise 4: Creating an Activity Diagram ......................................................................................... 6-16
Class Diagram From the Specification Perspective ......................................................................... 6-17
Module Summary ................................................................................................................................. 6-19
Exercise 5: Creating a Class Diagram From the Specification Perspective ................................... 6-20
Check Your Progress ............................................................................................................................ 6-21
Think Beyond ........................................................................................................................................ 6-22

Implementing Java Security


Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services December 2000 Revision A.3

vi

You might also like