You are on page 1of 175

Sun Educational Services

Migrating to OO Programming
With Java Technology

SL-210/SL-211

Migrating to OO Programming With Java Technology April 1999


Sun Educational Services

Preface

About This Course

Migrating to OO Programming With Java Technology April 1999


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/276: Java Programming Language.

Migrating to OO Programming With Java Technology Preface, slide 2 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Preface, slide 3 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Preface, slide 4 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services
Course Map

Object-Oriented Concepts

Objects Classes

Implementing Object-Oriented Concepts


in the Java Programming Language

Using Java Using Java


Classes Methods

Object Relationships

Object Interaction

Object-Oriented Notation

Object-Oriented
Analysis and Design
Using UML

Migrating to OO Programming With Java Technology Preface, slide 5 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, April 1999, Revision A.1
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 Preface, slide 6 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Preface, slide 7 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Preface, slide 8 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Skills Gained by Module


Meaning of:
• Black boxes
• Gray boxes

Module
Skills Gained 1 2 3 4
Skill or Objective 1
Skill or Objective 2
Skill or Objective 3
Skill or Objective 4

Migrating to OO Programming With Java Technology Preface, slide 9 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Guidelines for Module Pacing

Module Day 1 Day 2 Day 3

About This Course A.M.


Objects A.M.
Classes A.M./P.M.
Using Java Classes P.M.
Using Java Methods A.M.
Object Interaction A.M./P.M.
Object-Oriented Analysis and Design A.M./P.M.
Using UML

Migrating to OO Programming With Java Technology Preface, slide 10 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Topics Not Covered


• Advanced Java technology programming – Covered in
SL-275/276: Java Programming Language
• Advanced OO concepts – Covered in OO-226: Object-
Oriented Application Analysis and Design for Java
Technology (UML)

Migrating to OO Programming With Java Technology Preface, slide 11 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

How Prepared Are You?


• Six months or more of experience programming
• Experience designing applications

Migrating to OO Programming With Java Technology Preface, slide 12 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Preface, slide 13 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Preface, slide 14 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Icons
• Demonstration

• Reference

• Discussion

• Exercise

Migrating to OO Programming With Java Technology Preface, slide 15 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Typographical Conventions
Typeface or
Meaning Example
Symbol
AaBbCc123 The names of commands, files, Edit your .login file.
and directories; Use ls -a to list all files.
on-screen computer output system% You have mail.
AaBbCc123 What you type, contrasted with system% su
on-screen computer output Password:
AaBbCc123 Command-line placeholder— To delete a file, type rm
replace with a real value filename.
AaBbCc123 Book titles, new words or Read Chapter 6 in User’s
terms, or words to be Guide. These are called class
emphasized options.
You must be root to do this.

Migrating to OO Programming With Java Technology Preface, slide 16 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Preface, slide 17 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Module 1

Objects

Migrating to OO Programming With Java Technology April 1999


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 Module 1, slide 2 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 1, slide 3 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 1, slide 4 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 1, slide 5 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 1, slide 6 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Identifying Objects

Satellite dish Employee Painter’s palette

X + Yi
Complex number Bank account Bicycle

• Example objects

Migrating to OO Programming With Java Technology Module 1, slide 7 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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
• check

Migrating to OO Programming With Java Technology Module 1, slide 8 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, April 1999, Revision A.1
Sun Educational Services

Identifying Object Attributes and


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

Migrating to OO Programming With Java Technology Module 1, slide 9 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 1, slide 10 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 1, slide 11 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 1, slide 12 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 1, slide 13 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 1, slide 14 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 1, slide 15 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 1, slide 16 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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-9 that do not have
attributes and operations?

Migrating to OO Programming With Java Technology Module 1, slide 17 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 1, slide 18 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 1, slide 19 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Implementing Encapsulation
• An object’s 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 Module 1, slide 20 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 1, slide 21 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Exercise: Identifying Objects


• Exercise objective
• Preparation
• Tasks
• Exercise summary

Migrating to OO Programming With Java Technology Module 1, slide 22 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 1, slide 23 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 1, slide 24 of 24


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Module 2

Classes

Migrating to OO Programming With Java Technology April 1999


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 Module 2, slide 2 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Class Overview

Objects of Objects of a
a Leaf class 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 Module 2, slide 3 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 2, slide 4 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 2, slide 5 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 2, slide 6 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

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

Subclasses Basketball Soccer Ball Baseball

Migrating to OO Programming With Java Technology Module 2, slide 7 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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

Migrating to OO Programming With Java Technology Module 2, slide 8 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services
Case Study
Both the Customer and Employee classes
extend Person.

attributes
ID
firstName
lastName
initial
methods
giveName
Person add
remove

attributes
ID
Customer Employee firstName
lastName
attributes initial
ID *SSN
firstName methods
lastName giveName
initial add
*salutation remove
methods OE Clerk CS Rep.
giveName
add
remove

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 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, April 1999, Revision A.1
Sun Educational Services

Specialization
Generalized Transport
class:

Classes: Automobile Bicycle Plane

Specialized Commercial
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 Module 2, slide 10 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 2, slide 11 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Case Study
• In the order-entry system, you could use polymorphism
with the Person object’s 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 Module 2, slide 12 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Abstract Classes
• Abstract classes define the attributes and operations
that must be implemented.
• Abstract classes provide a default operation definition
and expect every subclass to provide its own actions to
be performed for the operation defined.
• Operations in an abstract class are placeholders.

Migrating to OO Programming With Java Technology Module 2, slide 13 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 2, slide 14 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Exercise: Grouping Objects in Classes


• Exercise objective
• Preparation
• Tasks
• Exercise summary

Migrating to OO Programming With Java Technology Module 2, slide 15 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 2, slide 16 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 2, slide 17 of 17


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Module 3

Using Java Classes

Migrating to OO Programming With Java Technology April 1999


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 Module 3, slide 2 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 3, slide 3 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Declaring Classes
[class_modifier] class class_identifier
{block}

• class_modifier – optional; public, abstract, or


final

• class – text 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 Module 3, slide 4 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

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

• Discussion – How would you code the class declaration


for the other classes?

Migrating to OO Programming With Java Technology Module 3, slide 5 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 3, slide 6 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 (A–Z)
• A lowercase letter (a–z)
• The underscore character (_)
• The dollar character ($)

Migrating to OO Programming With Java Technology Module 3, slide 7 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 (0–9)
• 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 Module 3, slide 8 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 3, slide 9 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 3, slide 10 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 3, slide 11 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Floating Point Types


• Floating point types store numbers with values to the
right of the decimal point.
Name or Type Float Length
float 32 bits
double 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 Module 3, slide 12 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 3, slide 13 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 3, slide 14 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Reference Types
• Is used to store the address of an object
• Can store only one reference type: a reference to an
object or the type of the subclass or superclass

Migrating to OO Programming With Java Technology Module 3, slide 15 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services
Reference Types
• Declaring a reference type:
type variable_identifier
Computer laptop;
• Creating objects using a reference
type:
variable_identifier = new
class_name
laptop = new Computer();
• Using reference types
int first = 9;
int second = first;
Computer laptop = new
Computer();
Computer
anotherReferenceToLaptop =
laptop;

Migrating to OO Programming With Java Technology Module 3, slide 16 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, April 1999, Revision A.1
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 Module 3, slide 17 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Case Study
• The Person class declaration with the variables defined
would appears as shown:
1 class Person
2 {
3 String firstName;
4 String lastName;
5 String initial;
6 String ID;
7 Address homeAddress
8 }

• How would you code the class declaration and


variables for the Order class?

Migrating to OO Programming With Java Technology Module 3, slide 18 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 3, slide 19 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 3, slide 20 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Inheritance
• Example: Manager and Clerk 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 Module 3, slide 21 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, April 1999, Revision A.1
Sun Educational Services
Using Inheritance
• Syntax for inheritance:
class subclass_name extends superclass_name
{block}

• Example:
1 class Employee
2 {
3 int employeeNumber;
4 String name;
5 int departmentNumber;
6 int extensionNumber;
7 int salary;
8 // and so on
9 }

1 class Manager extends Employee


2 {
3 int numberOfWorkers;
4 // and so on
5 }
6
7 class Engineer extends Employee
8 {
9 Manager worksFor;
10 // and so on
11 }

Migrating to OO Programming With Java Technology Module 3, slide 23 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, April 1999, Revision A.1
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 Module 3, slide 24 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services
Testing Inheritance
• Class declaration with duplication:
1 class Cycle
2 {
3 int numberOfWheels;
4 int numberOfSeats;
5 float luggageCapacity;
6 // and so on
7 }
8 class Boeing747
9 {
10 int numberOfWheels;
11 int numberOfSeats;
12 float luggageCapacity;
13 int numberOfWings;
14 // and so on
15 }

• First attempt to use inheritance:


1 class Cycle
2 {
3 int numberOfWheels;
4 int numberOfSeats;
5 float luggageCapacity;
6 // and so on
7 }
8 class Boeing747 extends Cycle
9 {
10 int numberOfWings;
11 // and so on
12 }

Migrating to OO Programming With Java Technology Module 3, slide 25 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, April 1999, Revision A.1
Sun Educational Services
Testing Inheritance
• Correct use of inheritance:
1 class Vehicle
2 {
3 int numberOFWheels;
4 int numberOfSeats;
5 float luggageCapacity;
6 // and so on
7 }
8
9 class Cycle extends Vehicle
10 {
11 // and so on
12 }
13
14 class Boeing747 extends Vehicle
15 {
16 int numberOfWings;
17 // and so on
18 }

Migrating to OO Programming With Java Technology Module 3, slide 26 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, April 1999, Revision A.1
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 Module 3, slide 27 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 3, slide 28 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 3, slide 29 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, April 1999, Revision A.1
Sun Educational Services

Abstract Classes and Inheritance


• Example: A Drawing class contains methods for a
variety of drawing features, implemented in a platform-
independent 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 Module 3, slide 31 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 3, slide 32 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 3, slide 33 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 3, slide 34 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 3, slide 35 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 // Class Employee of the Finance department for the


2 // ABC company
3 package abc.financedept;
4
5 public class Employee
6 {
7 ...
8 }

• All classes in the source file are within the named


package.

Migrating to OO Programming With Java Technology Module 3, slide 36 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 3, slide 37 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 // Class Manager belongs to the default package.
2 import abc.financeDept.*;
3 public class Manager extends Employee
4 {
5 String department;
6 Employee [] subordinates;
7 }

Migrating to OO Programming With Java Technology Module 3, slide 38 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 3, slide 39 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 OrderEntry package:
1 package OrderEntry;
2 import OrderEntry.Person;
3 import OrderEntry.Address;
4
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 Module 3, slide 40 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Exercise: Implementing the Class


Diagram
• Exercise objective
• Preparation
• Tasks
• Exercise summary

Migrating to OO Programming With Java Technology Module 3, slide 41 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 3, slide 42 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 3, slide 43 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 3, slide 44 of 44


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Module 4

Using Java Methods

Migrating to OO Programming With Java Technology April 1999


Sun Educational Services

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

Migrating to OO Programming With Java Technology Module 4, slide 2 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 4, slide 3 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 4, slide 4 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 4, slide 5 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, April 1999, Revision A.1
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 }
10
11class ClassOne
12{
13 public void returnNothing()
14 {
15 //whatever the method does
16 }
17}

Migrating to OO Programming With Java Technology Module 4, slide 7 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services
Case Study
• Customer.java source file:
1 package OrderEntry;
2
3 public class Customer extends Person
4 {
5 public static void main (String args[])
6 {
7 Adress shipTo = new Address();
8 shipTo.printLabel();
9 }
10 }

• Address.java source file:


1 package OrderEntry;
2
3 public class Address
4 {
5 private String addressLine1;
6 private String addressLine2;
7 private String city;
8 private String state;
9 private String postalCode;
10 private String country;
11
12 public void printLabel()
13 {
14 System.out.println(addressLine1);
15 if (addressLine2 != null)
16 System.out.println(addressLine2);
17 System.out.println(city + ", "+ state +
18 postalCode + country);
19 }
20 }

Migrating to OO Programming With Java Technology Module 4, slide 8 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, April 1999, Revision A.1
Sun Educational Services

Object Methods
• Method must be preceded by the reference to the object
and the dot operator (“.”).
1 class MethodInvocation
2 {
3 public static void main (String args[])
4 {
5 ClassOne one = new ClassOne();
6 int i = one.returnAnInt();
7 int j = i * one.returnAnInt() + 6;
8 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 Module 4, slide 9 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 class StaticExample
2 {
3 public static void main (string args[])
4 {
5 double rand = Math.random();
6 System.out.println("A random number: " + rand);
7 }
8 }

Migrating to OO Programming With Java Technology Module 4, slide 10 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Case Study
Discussion – What static methods could you use in the order-
entry example?

Migrating to OO Programming With Java Technology Module 4, slide 11 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 4, slide 12 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 4, slide 13 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 4, slide 14 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 9, passing
the homeAddress to set the shipTo attribute.
1 package OrderEntry;
2 import OrderEntry.Person;
3 import OrderEntry.Address;
4
5 public class Customer extends Person {
6 private Address shipTo;
7
8 public void initialize() {
9 setShipTo(Address homeAddress);
10 }
11
12 public Address getShipTo(){}
13 public void setShipTo(Address addr){}
14 }

Migrating to OO Programming With Java Technology Module 4, slide 15 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 public abstract class Person {
2 private String firstName;
3 private String lastName;
4 private String initial;
5 private int ID;
6 private Address homeAddress;
7
8 public String getName(){return firstName;}
9 public void setFirstName(String fName){}
10 public void setLastName(String lName){}
11 public void setInitial(String init){}
12 public int getID(){return ID;}
13 public void setID(String id){}
14 public Address getAddress(){return homeAddress;}
15 public void setAddress(Address addr){}
16 }

Migrating to OO Programming With Java Technology Module 4, slide 16 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 method’s
signature.

Migrating to OO Programming With Java Technology Module 4, slide 17 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services
Using Method
Overloading
1 class Example
2 {
3 public static void main (String args[])
4 {
5 StoreVal store = new StoreVal();
6 store.setValue(9);
7 int i = store.getValuePlus(2);
8 double d = store.getValuePlus(3.6);
9 }
10 }
11 class StoreVal
12 {
13 int value;
14 void setValue(int val)
15 {
16 value = val;
17 }
18 int getValuePlus(int more)
19 {
20 return (value + more);
21 }
22 double getValuePlus(double more)
23 {
24 return (value + more);
25 }
26 }

Migrating to OO Programming With Java Technology Module 4, slide 18 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, April 1999, Revision A.1
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 Module 4, slide 19 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 4, slide 20 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 4, slide 21 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 4, slide 22 of 30
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 4, slide 23 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Overloading Constructors
1 class Shirt
2 {
3 String type;
4
5 // Shirt() is the first constructor
6 Shirt()
7 {
8 name = "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 Module 4, slide 24 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 4, slide 25 of 30
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services
Case Study
The initialize method on line 9 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 package OrderEntry;
2 import OrderEntry.Person;
3 import OrderEntry.Address;
4
5 public class Customer extends Person
6 {
7 private Address shipTo;
8
9 // public void initialize()
10 // {
11 // setShipTo(Address homeAddress);
12 // }
13 Customer()
14 {
15 setShipTo(Address homeAddress);
16 }
17 public Address getShipTo() {}
18 public void setShipTo(Address addr){}
19 }

Migrating to OO Programming With Java Technology Module 4, slide 26 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, April 1999, Revision A.1
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 Module 4, slide 27 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Exercise: Develop Methods and


Constructors
• Exercise objective
• Preparation
• Tasks
• Exercise summary

Migrating to OO Programming With Java Technology Module 4, slide 28 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Check Your Progress


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

Migrating to OO Programming With Java Technology Module 4, slide 29 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 4, slide 30 of 30


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Module 5

Object Interaction

Migrating to OO Programming With Java Technology April 1999


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 Module 5, slide 2 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 5, slide 3 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services
Case Study
• Customer.java source file:
1 package OrderEntry;
2
3 public class Customer extends Person
4 {
5 public static void main (String args[])
6 {
7 Address shipTo = new Address();
8 shipTo.printLabel();
9 }
10 }

• Address.java source file:


1 package OrderEntry;
2 public class Address
3 {
4 private String addressLine1;
5 private String addressLine2;
6 private String city;
7 private String state;
8 private String postalCode;
9 private String country;
10
11 public void printLabel()
12 {
13 System.out.println(addressLine1);
14 if (addressLine2 != null)
15 System.out.println(addressLine2);
16 System.out.println(city + “, “ + state +
17 postalCode + country);
18 }
19 }

Migrating to OO Programming With Java Technology Module 5, slide 4 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, April 1999, Revision A.1
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 Module 5, slide 5 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 5, slide 6 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 setRefrigerator (Refrigerator aRefrigerator)
12 {
13 myRefrigerator = aRefrigerator;
14 }
15 }

Migrating to OO Programming With Java Technology Module 5, slide 7 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, April 1999, Revision A.1
Sun Educational Services
Implementing Association
and Composition
• Composition:
1 class Seat
2 {
3 //whatever the class does
4 }
5
6 class Wheel
7 {
8 // whatever the class does
9 }
10
11 class Frame
12 {
13 // whatever the class does
14 }
15
16 class Car
17 {
18 Seat mySeat = new Seat();
19 Wheel myWheel = new Wheel();
20 Frame myFrame = new Frame();
21 // and so on
22 }

Migrating to OO Programming With Java Technology Module 5, slide 8 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, April 1999, Revision A.1
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 Module 5, slide 9 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Contrasting Composition and


Association
Composition Association
Implements the “has a” re- Implements the “uses a”
lationship relationship
Management is simpler, Objects must be created
components are created Associations and custody
automatically must be established
Inflexible Flexible
Useful when an object has
interchangeable compo-
nents or when an object al-
ready exists

Migrating to OO Programming With Java Technology Module 5, slide 10 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 5, slide 11 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 5, slide 12 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 5, slide 13 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Exercise: Modeling Object


Relationships
• Exercise objective
• Preparation
• Tasks
• Exercise summary

Migrating to OO Programming With Java Technology Module 5, slide 14 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 5, slide 15 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 5, slide 16 of 16


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Module 6

Object-Oriented Analysis and Design


Using UML

Migrating to OO Programming With Java Technology April 1999


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 Module 6, slide 2 of 20


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services

Object-Oriented Analysis and Design


• This module provides a simplified approach to object-
oriented 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 Module 6, slide 3 of 20


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 6, slide 4 of 20


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services
Use Case Diagrams
Enter order
Customer

<<uses>>
CSR

OEC Check levels


Inventory
system

Assemble order
Warehouse

Legend

Use case Link

Actor

Migrating to OO Programming With Java Technology Module 6, slide 5 of 20


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, April 1999, Revision A.1
Sun Educational Services

Exercise 1: Writing a Use Case


Description
• Exercise objective
• Tasks
• Exercise summary

Migrating to OO Programming With Java Technology Module 6, slide 6 of 20


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 6, slide 7 of 20


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services
Sequence Diagram

Order Order Order Item Payment


Window Line
initialize()
*[for each orderline]
initialize()
inStock:= check()

[inStock]
add()

reorder:= reorder()

[not inStock]
hold()

verify()

Legend

Object [...] Condition Activation

* Iteration marker X Deletion


Message Return

Migrating to OO Programming With Java Technology Module 6, slide 8 of 20


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, April 1999, Revision A.1
Sun Educational Services

Exercise 2: Creating a Sequence


Diagram
• Exercise objective
• Preparation
• Tasks
• Exercise summary

Migrating to OO Programming With Java Technology Module 6, slide 9 of 20


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 6, slide 10 of 20


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services
Class Diagram From the
Conceptual Perspective
Order Customer Address
* 1 * 2
dateReceived shipTo: Address addressLine1
isPrepaid: boolean billTo: Address addressLine2
number: String city
state
postalCode
country
1
1 *
1
Employee Person
0..1 manager ID
department firstName
dependents lastName
SSN initial
homeAddr:
Address
*

Order Line Item


* 1
quantity: Integer number: String
price: Money description: String
unitPrice: Money

Legend

Classes Relationships

0..1, 1, * Multiplicities

Migrating to OO Programming With Java Technology Module 6, slide 11 of 20


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, April 1999, Revision A.1
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 Module 6, slide 12 of 20


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 6, slide 13 of 20


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services
Activity Diagrams
Enter
Order

* [for each orderline]

Cancel [failed] Verify [out of stock] Hold


Check
Order Payment Order
Item

[in stock]

Assign
[succeeded]
to Order

[all items
assigned
and payments
verified]

Dispatch
Order

Legend

Activity [...] Guard

Trigger Synchronization bar

Migrating to OO Programming With Java Technology Module 6, slide 14 of 20


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, April 1999, Revision A.1
Sun Educational Services

Exercise 4: Creating an Activity


Diagram
• Exercise objective
• Tasks
• Exercise summary

Migrating to OO Programming With Java Technology Module 6, slide 15 of 20


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 6, slide 16 of 20


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
Sun Educational Services
Class Diagram From the
Specification Perspective
Order Customer Address
* 1 * 2
dateReceived shipTo: Address addressLine1
isPrepaid: boolean billTo: Address addressLine2
number: String city
state
postalCode
country
1
1 *
1
Employee Person
0..1 manager ID
department firstName
dependents lastName
SSN initial
homeAddr:
promote() Address
*

Order Line Item


* 1
quantity: Integer number: String
price: Money description: String
unitPrice: Money

Legend

Classes Relationships

0..1, 1, * Multiplicities

Migrating to OO Programming With Java Technology Module 6, slide 17 of 20


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services, April 1999, Revision A.1
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 Module 6, slide 18 of 20


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 6, slide 19 of 20


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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 Module 6, slide 20 of 20


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision A.1
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
Identifying Objects .................................................................................................................................. 1-6
Case Study ................................................................................................................................................ 1-8
Identifying Object Attributes and Operations .................................................................................... 1-9
Case Study .............................................................................................................................................. 1-10

Migrating to OO Programming With Java Technology xxi


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision Revision A.1
Sun Educational Services

Testing an Identified Object ................................................................................................................ 1-11


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

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
Case Study .............................................................................................................................................. 2-12
Abstract Classes ..................................................................................................................................... 2-13
Case Study .............................................................................................................................................. 2-14
Exercise: Grouping Objects in Classes ............................................................................................... 2-15
Check Your Progress ............................................................................................................................ 2-16
Think Beyond ........................................................................................................................................ 2-17

Migrating to OO Programming With Java Technology xxii


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision Revision A.1
Sun Educational Services

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
Reference Types .................................................................................................................................... 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
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

Migrating to OO Programming With Java Technology xxiii


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision Revision A.1
Sun Educational Services

Using Classes Stored in Packages ....................................................................................................... 3-38


Coding Structure ................................................................................................................................... 3-39
Case Study .............................................................................................................................................. 3-40
Exercise: Implementing the Class Diagram ...................................................................................... 3-41
Check Your Progress ............................................................................................................................ 3-42
Think Beyond ........................................................................................................................................ 3-44

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
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

Migrating to OO Programming With Java Technology xxiv


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision Revision A.1
Sun Educational Services

Coding Structure ................................................................................................................................... 4-27


Exercise: Develop Methods and Constructors .................................................................................. 4-28
Check Your Progress ............................................................................................................................ 4-29
Think Beyond ........................................................................................................................................ 4-30

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
Exercise: Modeling Object Relationships .......................................................................................... 5-14
Check Your Progress ............................................................................................................................ 5-15
Think Beyond ........................................................................................................................................ 5-16

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


Objectives ................................................................................................................................................. 6-2
Object-Oriented Analysis and Design .................................................................................................. 6-3
Use Case Diagrams ................................................................................................................................. 6-4
Exercise 1: Writing a Use Case Description ........................................................................................ 6-6
Sequence Diagrams ................................................................................................................................. 6-7
Exercise 2: Creating a Sequence Diagram ........................................................................................... 6-9
Class Diagram From the Conceptual Perspective ............................................................................ 6-10
Exercise 3: Creating a Class Diagram From the Conceptual Perspective ..................................... 6-12

Migrating to OO Programming With Java Technology xxv


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision Revision A.1
Sun Educational Services

Activity Diagrams ................................................................................................................................. 6-13


Exercise 4: Creating an Activity Diagram ......................................................................................... 6-15
Class Diagram From the Specification Perspective ......................................................................... 6-16
Exercise 5: Creating a Class Diagram From the Specification Perspective ................................... 6-18
Check Your Progress ............................................................................................................................ 6-19
Think Beyond ........................................................................................................................................ 6-20

Migrating to OO Programming With Java Technology xxvi


Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services April 1999, Revision Revision A.1

You might also like