Introduction To Classes and Objects
Introduction To Classes and Objects
Introduction To Classes and Objects
3
Introduction to
Classes and Objects
OBJECTIVES
In this chapter you will learn:
What classes, objects, methods and instance variables
are.
How to declare a class and use it to create an object.
How to declare methods in a class to implement the
class’s behaviors.
How to declare instance variables in a class to
implement the class’s attributes.
How to call an object’s method to make that method
perform its task.
The differences between instance variables of a class
and local variables of a method.
How to use a constructor to ensure that an object’s
data is initialized when the object is created.
The differences between primitive and reference types.
3.1 Introduction
3.2 Classes, Objects, Methods and Instance Variables
3.3 Declaring a Class with a Method and Instantiating an
Object of a Class
3.4 Declaring a Method with a Parameter
3.5 Instance Variables, set Methods and get Methods
3.6 Primitive Types vs. Reference Types
3.7 Initializing Objects with Constructors
3.8 Floating-Point Numbers and Type double
3.9 (Optional) GUI and Graphics Case Study: Using Dialog
Boxes
3.10 (Optional) Software Engineering Case Study: Identifying
the Classes in a Requirements Document
3.11 Wrap-Up
3.1 Introduction
• Classes
• Floating-Point numbers
Class GradeBook
Class GradeBook
• Method declarations
– Keyword public indicates method is available to
public
– Keyword void indicates no return type
– Access modifier, return type, name of method and
parentheses comprise method header
Class GradeBookTest
• Java is extensible
– Programmers can create new classes
• Class instance creation expression
– Keyword new
– Then name of class to create and parentheses
• Calling a method
– Object name, then dot separator (.)
– Then method name and parentheses
Fig. 3.3 | UML class diagram indicating that class GradeBook has a public
displayMessage operation.
• Method parameters
– Additional information passed to a method
– Supplied in the method call with arguments
• Scanner methods
– nextLine reads next line of input
– next reads next word of input
Fig. 3.6 | UML class diagram indicating that class GradeBook has a displayMessage
operation with a courseName parameter of UML type String.
• private keyword
– Used for most instance variables
– private variables and methods are accessible only
to methods of the class in which they are declared
– Declaring instance variables private is known as
data hiding
• Return type
– Indicates item returned by method
– Declared in method header
• Attributes
– Listed in middle compartment
– Attribute name followed by colon followed by
attribute type
• Return type of a method
– Indicated with a colon and return type after the
parentheses after the operation name
Fig. 3.9 | UML class diagram indicating that class GradeBook has a courseName attribute
of UML type String and three operations—setCourseName (with a name parameter of
UML type String), getCourseName (returns UML type String) and displayMessage.
• Types in Java
– Primitive
• boolean, byte, char, short, int, long, float,
double
– Reference (sometimes called nonprimitive types)
• Objects
• Default value of null
• Used to invoke an object’s methods
• Constructors
– Initialize an object of a class
– Java requires a constructor for every class
– Java will provide a default no-argument constructor
if none is provided
– Called when keyword new is followed by the class
name and parentheses
Fig. 3.12 | UML class diagram indicating that class GradeBook has a constructor that has
a name parameter of UML type String.
• Floating-point numbers
– float
– double
• Stores numbers with greater magnitude and precision
than float
• float
– Single-precision floating-point numbers
– Seven significant digits
• double
– Double-precision floating-point numbers
– Fifteen significant digits
• Format specifier %f
– Used to output floating-point numbers
– Place a decimal and a number between the percent
sign and the f to mandate a precision
Fig. 3.15 | UML class diagram indicating that class Account has a private balance
attribute of UML type Double, a constructor (with a parameter of UML type Double) and
two public operations—credit (with an amount parameter of UML type Double) and
getBalance (returns UML type Double).
Location Title—Exercise(s)
Section 3.9 Using Dialog Boxes—Basic input and output with dialog boxes
Section 4.14 Creating Simple Drawings—Displaying and drawing lines on the screen
Section 6.13 Colors and Filled Shapes—Drawing a bull’s-eye and random graphics
Exercise 11.18 Expanding the Interface—Using GUI components and event handling
Exercise 12.12 Adding Java 2D—Using the Java 2D API to enhance drawings
Fig. 3.16 | Summary of the GUI and Graphics Case Study in each chapter.
• Package javax.swing
– Contains classes to help create graphical user
interfaces (GUIs)
– Contains class JOptionPane
• Declares static method showMessageDialog for
displaying a message dialog
• Input dialog
– Allows user to input information
– Created using method showInputDialog from
class JOptionPane
Modeling Classes
Modeling Classes
Symbol Meaning
0 None
1 One
m An integer value
0..1 Zero or one
m, n m or n
m..n At least m, but not more than n
* Any non-negative integer (zero or more)
0..* Zero or more (identical to *)
1..* One or more
Modeling Classes
Fig. 3.26 | Class diagram for the ATM system model including class Deposit.