You are on page 1of 53

| 



|

Presentation slides for

Java Software Solutions


Foundations of Program Design
Third Edition

by John Lewis and William Loftus

Java Software Solutions is published by Addison-Wesley

Presentation slides are copyright 2002 by John Lewis and William Loftus. All rights reserved.
Instructors using the textbook may use and modify these slides for pedagogical purposes.



|
u Œow we can explore various aspects of classes and
objects in more detail

u Chapter 5 focuses on:


Ñ object references and aliases
Ñ passing objects references as parameters
Ñ the static modifier
Ñ wrapper classes
Ñ nested classes and inner classes
Ñ interfaces
Ñ dialog boxes
Ñ GUI components, events, and listeners

¢
D

u Decall from Chapter 2 that an object reference
variable holds the memory address of an object

u Dather than dealing with arbitrary addresses, we


often depict a reference graphically as a ³pointer´ to
an object
|  
   | 

 

-
r 
D

u An object reference variable that does not currently
point to an object is called a  
u The reserved word  can be used to explicitly set
a null reference:
  

or to check to see if a reference is currently null:


   
    
r 
D

u An object reference variable declared at the class
level (an instance variable) is automatically initialized
to null

u The programmer must carefully ensure that an object


reference variable refers to a valid object before it is
used

u Attempting to follow a null reference causes a


Π   to be thrown

u Usually a compiler will check to see if a local variable


is being used without being initialized
r  D

u The   reference allows an object to refer to itself
u That is, the   reference, used inside a method,
refers to the object through which the method is
being executed
u Suppose the   reference is used in a method
called !
u If ! is invoked as follows, the   reference
refers to "
:
"
!
u §ut in this case, the   reference refers to "#:
"#!
r  

u The   reference can also be used to distinguish
the parameters of a constructor from the
corresponding instance variables with the same
names
$  % & %Œ&
 
'
    
 Œ Œ
    
(


D  
u The act of assignment takes a copy of a value and
stores it in a variable

u For primitive types:


#  


§   

# 
#

)
# ) )

`
D



u For object references, assignment copies the
memory location:

  #  


§   
 
  #  
  #

v
 
u Two or more references that refer to the same object
are called pp  of each other

u ^ne object (and its data) can be accessed using


different reference variables

u Aliases can be useful, but should be managed


carefully

u Changing the object¶s state (its variables) through


one reference changes it for all of its aliases

 
r
   
u The == operator compares object references for
equality, returning  if the references are aliases
of each other

 
   #

u A method called * is defined for all objects, but


unless we redefine it when we write a class, it has the
same semantics as the == operator

 
*  #

u We can redefine the * method to return 


under whatever conditions we think are appropriate
è |  

u When an object no longer has any valid references to


it, it can no longer be accessed by the program

u The object is useless, and therefore is called uppu

u Java performs p
p uppu
 

periodically, returning an object's memory to the
system for future use

u In other languages, the programmer is responsible


for performing garbage collection

 ¢
 
u Parameters in a Java method are ?p   p 

u This means that a copy of the actual parameter (the


value passed in) is stored into the formal parameter
(in the method header)

u Passing parameters is therefore similar to an


assignment statement

u When an object is passed to a method, the actual


parameter and the formal parameter become aliases
of each other

  
u What you do with a parameter inside a method may
or may not have a permanent effect (outside the
method)

u See ParameterPassing.java (page 277)


u See ParameterTester.java (page 279)
u See Œum.java (page 280)

u Œote the difference between changing the reference


and changing the object that the reference points to
r    
u In Chapter 2 we discussed static methods (also
called class methods) that can be invoked through
the class name rather than through a particular
object
u For example, the methods of the ! class are
static:
! *#)

u To write a static method, we apply the 


modifier to the method definition
u The  modifier can be applied to variables as
well
 

u It associates a variable or method with the class


!  
u Static variables are also called p pp
u Œormally, each object has its own data space, but if a
variable is declared as static, only one copy of the
variable exists

 

u wemory space for a static variable is created when


the class in which it is declared is loaded
u All objects created from the class share static
variables
u Changing the value of a static variable in one object
changes it for all others  
!  

- 

    


'
 
  +,
 
(

§ecause it is static, the method can be invoked as:

 -  )

 
!  
u The order of the modifiers can be interchanged, but
by convention visibility modifiers come first
u Decall that the  method is static; it is invoked by
the system without creating an object

u Static methods cannot reference instance variables,


because instance variables don't exist until an object
exists

u However, a static method can reference static


variables or local variables

 `
r    
u Static methods and static variables often work
together

u See CountInstances.java (page 284)

u See Slogan.java (page 285)


ï|
u A Op??p represents a particular primitive type

u For example
 %%."    %#/

uses the  % class to create an object which


effectively represents the integer 20 as an object

u This is useful when a program requires an object


instead of a primitive type
ï|
u There is a wrapper class in the " % package
for each primitive type:

Primitive Type Wrapper Class


 0
   
   %
 % 1 %
 2
 3
  | 
 0
 4
ï|
u Wrapper classes contain static methods that help
manage the associated type
u For example, the  % class contains a method to
convert an integer stored in a  % to an  
value:
  %  

u The wrapper classes often contain useful static


constants as well
u For example, the  % class contains !Π4$15
and !$6 4$15 which hold the smallest and largest
  values
Ë
D  
u The Ë class was introduced in Chapter 2 to
facilitate capturing input from the keyboard
u Decall that the Ë class was written by the
authors of the book
u The Ë class hides various aspects of Java
input processing

u §ut it is important to understand how that processing


works
D
Ë

u Java I/^ is accomplished using objects that
represent streams of data

u A p is an ordered sequence of bytes


u The  object represents a standard
? 
p , which defaults to the monitor screen

u Deading keyboard input is more complicated

u The ?  p is made up of multiple objects:


07    07
  7 
D
Ë

u The  object is used to create an
 7 object
u The  7 object is used to create a
07 object
u This creates an input stream that treats input as
characters and buffers them so that input can be
read a line at a time
u The 1  method of the 07 class
reads an entire line of input as a  %
u Wrapper class methods can be used to convert
numeric input
D
Ë

u Problems that arise in reading or converting a value
manifest themselves as exceptions

u The 
O p  of a method header indicates what
exceptions it may throw

u See Wages2.java (page 289)

u I/^ and exceptions are explored further in Chapter 8


Œ|
u In addition to containing data and methods, a class
can contain other classes

u A class declared within another class is called a


  p

Enclosing Class

Œested Class
Œ|
u A nested class has access to the variables and
methods of the enclosing class, even if they are
declared private

u In certain situations this makes the implementation


of the classes easier because they can share
information easily

u Furthermore, the nested class can be protected by


the enclosing class from external use

u This is a special relationship and should be used with


care
Œ|
u A nested class produces a separate bytecode file
u If a nested class called   is declared in an
outer class called ., two bytecode files are
produced:
.

.8 

u Œested classes can be declared as static, in which


case they cannot refer to instance variables or
methods


|
u A nonstatic nested class is called an p

u An inner class is associated with each instance of


the enclosing class

u An instance of an inner class can exist only within an


instance of an enclosing class

u See TestInner.java (page 291)

u See ^uter.java (page 292)



 
u A Java  p is a collection of abstract methods
and constants

u An p p   
is a method header without a
method body

u An abstract method can be declared using the


modifier , but because all methods in an
interface are abstract, usually it is left off

u An interface is used to establish, as a formal


contract, a set of methods that a class will implement

 

interface is a reserved word


Œone of the methods in
an interface are given
 3 a definition (body)
'
9 
 9 
9 #&  
 9 .   
(

A semicolon immediately
follows each method header

 
u An interface cannot be instantiated

u wethods in an interface have public visibility by


default

u A class formally implements an interface by


Ñ stating so in the class header

Ñ providing implementations for each abstract method in the


interface

u If a class asserts that it implements an interface, it


must define all methods in the interface

 

| 3  3


'
9 
implements is a
'
reserved word
:: 
(

9  Each method listed


' in Doable is
::  given a definition
(

::
(

 
u A class that implements an interface can implement
other methods as well

u See Complexity.java (page 294)


u See Question.java (page 295)
u See winiQuiz.java (page 297)

u In addition to (or instead of) abstract methods, an


interface can contain constants

u When a class implements an interface, it gains


access to all its constants

 
u A class can implement multiple interfaces

u The interfaces are listed in the implements clause

u The class must implement all methods in all


interfaces listed in the header

! 9  %   


& #
'
::   
(

 
u The Java standard class library contains many
helpful interfaces
u The |  interface contains an abstract
method called  9, which is used to compare
two objects
u The  % class implements | , giving us
the ability to put strings in lexicographic order
u The  interface contains methods that allow
the user to move easily through a collection of
objects
r |
 
u The |  interface provides a common
mechanism for comparing one object to another
"
 9"#;/
   <"
  "#=

u The result is negative is "


is less that "#, 0 if
they are equal, and positive if "
is greater than
"#

u When a programmer writes a class that implements


the |  interface, it should follow this intent

u It's up to the programmer to determine what makes


one object less than another
r 
 
u The  interface provides a means of moving
through a collection of objects, one at a time
u The Π method returns a boolean result (true if
there are items left to process)
u The   method returns the next object in the
iteration
u The  method removes the object most
recently returned by the   method
   
u A p
u
 is a graphical window that pops up on
top of any currently active window for the user
u The Swing API contains a class called —.   
that simplifies the creation and use of basic dialog
boxes
u There are three categories of —.    dialog
boxes
Ñ A  pu p
u displays an output string
Ñ An ?  p
upresents a prompt and a single input text
field
Ñ A 
  p
u presents the user with a simple yes-or-no
question

u See Even^dd.java (page 302)


r  
 
è !
 
u A Graphical User Interface (GUI) is created with at
least three kinds of objects
Ñ components
Ñ events
Ñ listeners

u A GUI 
?
 defines a screen element to
display information or allow the user to interact with
the program
Ñ buttons, text fields, labels, menus, etc.

u A 
 p is a special component that holds and
organizes other components
Ñ dialog boxes, applets, frames, panels, etc.


u An  is an object that represents some activity to
which we may want to respond

u For example, we may want our program to perform


some action when the following occurs:
Ñ the mouse is moved
Ñ a mouse button is clicked
Ñ the mouse is dragged
Ñ a graphical button is clicked
Ñ a keyboard key is pressed
Ñ a timer expires

u Events often correspond to user actions, but not


always


" 

u The Java standard class library contains several
classes that represent typical events

u Components, such as an applet or a graphical


button, generate (fire) an event when it occurs

u ^ther objects, called   , wait for events to


occur

u We can write listener objects to do whatever we want


when an event occurs

u A listener object is often defined using an inner class




" 




|   ° 

This object may This object waits for and


generate an event responds to an event

When an event occurs, the generator calls


the appropriate method of the listener,
passing an object that describes the event
" 

 
u We can create a listener object by writing a class that
implements a particular   p

u The Java standard class library contains several


interfaces that correspond to particular event
categories

u For example, the !1  interface contains


methods that correspond to mouse events

u After creating the listener, we p the listener to the


component that might generate the event to set up a
formal relationship between the generator and
listener
|
è!
u To create a program with a GUI:
Ñ define and set up the components
Ñ create listener objects
Ñ set up the relationships between the listeners and the
components which generate events of interest
Ñ define what happens in response to each event

u A ? 
 is a component that allows the user to
initiate an action with the press of the mouse button
Ñ defined by the —0 class
Ñ generates an p 


u A pis a component that displays a line of text (or


an image, or both)
Ñ defined by the —1 class
|
è!
u The   method of an applet can be used to set up
the GUI and add each component to the applet
container
u The Swing version of the $  class is called
—$ 

u In a —$ , components are added to the applet's



  ?p

u The content pane is retrieved using the


%|    method

u A —0 generates an p 


u See PushCounter.java (page 305)


r  |
 
  
" 

u The interface corresponding to an action event is
called $ 1 , which defines only one
method, called  

u The 0 1  inner class implements the


$ 1  interface in the  0
program
u When the button is pushed, the —0 object
invokes the   method, passing it an
$  

u The listener method may or may not make use of the


event object passed to it
è!  

u A p  is a container component used for stand-
alone GUI-based applications

u A ?p is a container, but unlike a frame, it cannot


be displayed on its own
Ñ it must be added to another container

Ñ it helps organize the components in a GUI

u See Fahrenheit.java (page 309)

u See FahrenheitGUI.java (page 310)


r # 
  
!
u Chapter 5 has focused on:
Ñ object references and aliases
Ñ passing objects references as parameters
Ñ the static modifier
Ñ wrapper classes
Ñ nested classes and inner classes
Ñ interfaces
Ñ dialog boxes
Ñ GUI components, events, and listeners

You might also like