Java Programming,
10e
Chapter 04: Using Classes and
Objects
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
1
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Chapter Objectives
By the end of this chapter, you should be able to:
04.01 Describe classes and objects
04.02 Create a class
04.03 Create instance methods in a class
04.04 Declare objects and use their methods
04.05 Compare classes to data types
04.06 Create and use constructors
04.07 Use the this reference
04.08 Use static fields
04.09 Use imported, prewritten constants and methods
04.10 Use composition and nest classes
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.1 Learning About Classes and Objects (1 of 2)
Java Classes can be differentiated into two types
• Classes from which objects are not instantiated
• Classes from which you create objects
In object-oriented programming:
• Everything is an object
• Every object is a member of a class
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.1 Learning About Classes and Objects (2 of 2)
is-a relationship
• A relationship in which the object “is a” concrete example of the class
• An object is an instantiation, or tangible example, of a class
Objects and classes
• Classes provide reusability
• Objects get their attributes from their classes
• You don’t need to create ever class you use
Class client or Class user
• An application or class that instantiates objects of another class
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 4.1: Knowledge Check
An object is a(n) ___________ of a class
a. instantiation
b. dependent
c. field
d. subclass
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 4.1: Knowledge Check Answer
An object is a(n) ___________ of a class
Answer: a. instantiation
An object is an instantiation of a class, or one tangible example of a class. Your
goldfish, my guppy, and the zoo’s shark each constitute one instantiation of the
Fish class.
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.2 Creating a Class (1 of 4)
Class header
• An optional access specifier
• The keyword class
• Any legal identifier
Public class
• Accessible by all objects
• Can be extended as a basis for another class
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.2 Creating a Class (2 of 4)
Data fields
• The data components of a class
• Distinguished from other variables you might use
Instance variable
• One copy exists for each object instantiation
• Each object gets its own copy of each nonstatic data field in its class
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.2 Creating a Class (3 of 4)
Private access
• No other classes can access the field’s values
• Only methods of the same class are allowed to use private variables
Information hiding
• Principle used in creating private access
• Private data can be changed only by a class’s own methods
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.2 Creating a Class (4 of 4)
When you create classes from which you will instantiate objects
• The class’s data fields are most often private
• The class’s methods are most often not static
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.3 Creating Instance Methods in a Class (1 of 5)
Classes from which you intend to instantiate objects typically contain
• Fields
• Methods
Mutator methods
• Set or change field values
Accessor methods
• Retrieve values
• Called setters (start with prefix set)
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.3 Creating Instance Methods in a Class (2 of 5)
Figure 4-2: The setEmpNum() and
getEmpNum() methods
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.3 Creating Instance Methods in a Class (3 of 5)
Nonstatic methods
• Used with object instantiations
• Called instance methods
• You can use either a static or nonstatic method
• Only nonstatic methods behave uniquely for each object
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.3 Creating Instance Methods in a Class (4 of 5)
Static Nonstatic
• Static is a keyword that can be used as an • Unless you declare a field or method static, it is
adjective nonstatic by default
• Static fields in a class are called class fields • Nonstatic fields in a class are called instance
variables
• Static methods in a class are calls class
methods • Nonstatic methods in a class are called
instance methods
• When you use a static field or method you do
not need to use an object • When you use a nonstatic field or method you
must use an object
• When you create a static method and
instantiate 100 objects, only one copy exists in • When you create a static method and
memory instantiate 100 objects, 100 copies exist in
memory
• Static class variables are not instance variables
• Instance fields and methods are nonstatic
[Author Name], [Book Title], [#] Edition. © [Insert Year] Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.3 Creating Instance Methods in a Class (5 of 5)
Most programmers place data fields in logical order
• At beginning of the class
• There is no requirement to do so
• Unique identifiers are similar to a database’s primary key
• You can place a class’s data fields and methods in any order
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 4.2: Discussion
1. Explain what it means for a public class to be extended. Why would a
programmer extend a public class?
2. Differentiate between mutator and accessor methods.
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 4.2: Discussion Debrief
1. Public classes also can be extended, or used as a basis for any other class.
Making access public means that if you develop a good Employee class,
and someday you want to develop two classes that are more specific,
SalariedEmployee and HourlyEmployee, then you do not have to start
from scratch. Each new class can become an extension of the original
Employee class, inheriting its data and methods.
2. Methods that set or change field values are called mutator methods;
methods that retrieve values are called accessor methods. In Java, mutator
methods are often called setters, and they conventionally start with the
prefix set. Accessor methods are called getters, and they conventionally
start with the prefix get.
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.4 Declaring Objects and Using Their Methods (1 of
2)
Declaring a class
• Does not create any actual objects
• A class is a description of what an object will be like when instantiated
Creating an object that is an instance of a class
• Supply a type and identifier
• Allocate computer memory for the object
• Use a new operator
• Reference to an object
Constructor
• Method that creates and initializes objects
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.4 Declaring Objects and Using Their Methods (2 of
2)
Data hiding
• Using encapsulation
• Data fields should be private and inaccessible from a client application
• Can rewrite a method to prevent invalid data
• Some fields clients should not be able to alter
• Assigned values
• Set directly using calculations
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.5 Understanding That Classes Are Data Types (1 of
3)
Abstract data type (ADT)
• A class you create
• Implementation is hidden and accessed through public methods
• Also called programmer-defined data type
• Not built into the language
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.5 Understanding That Classes Are Data Types (2 of
3)
Composite type
• A class composite of smaller parts
• Java’s 8 built-in data types are not composite
• Such as int and double
• Primitive types can also be called scalar
• Java’s creators have already defined
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.5 Understanding That Classes Are Data Types (3 of
3)
When creating types, programmers ask:
• What shall we call it?
• What are its attributes
• What methods are needed
When you declare a primitive type object
• You provide its type and identifier
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.6 Creating and Using Constructors (1 of 2)
Constructor
• Establishes an object
• Default constructor requires no arguments
• Automatically created by compositor if you don’t write a constructor
When you write a constructor
• Must have the same name as the class it constructs
• Cannot have a return type (even void)
• Normally declared to be public
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.6 Creating and Using Constructors (2 of 2)
Creating Constructors with Parameters
• Often parameters are used to initialize data fields for an object
• When the constructor executes, the integer within the calls is passed as the
parameter
• When you write a constructor you no longer receive the default
• Overloading constructors lets you create objects with different initializing
arguments (or none)
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 4.3: Discussion
1. What types of questions do Java programmers consider when creating
primitive data types?
2. What is the purpose of a constructor that receives parameters. What
specific purposes might this be used for?
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 4.3: Discussion Debrief
1. What shall we call it? What are its attributes? What method is needed by it?
Are there any other methods needed?
2. In addition to writing a default constructor for a class, you also can write
versions that receive parameters. Such parameters often are used to
initialize data fields for an object. One example is to assign an employee
number to an employee.
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.7 Learning About the this Reference (1 of 4)
Classes can become large very quickly
• Each class can have many methods, including overloaded methods
• When you instantiate an object from a class, memory is reserved for each
instance field
• Fortunately, objects can share some variables and methods
• In Java, just one copy of each nonstatic method in a class is stored
• All instantiated objects can use that copy
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.7 Learning About the this Reference (2 of 4)
this reference
• The reference to an object that is passed to any object’s nonstatic reference
• A reference is an object’s memory address
• this is a reserved word in Java
• Only nonstatic instance methods can have a this reference
• When you use a nonstatic method, you use the object name, a dot, and the method
name
• Usually, you neither want nor need to refer to the this reference within the
instance method
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.7 Learning About the this Reference (3 of 4)
Figure 4-19: A Student class whose constructor does not work
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.7 Learning About the this Reference (4 of 4)
Using the this reference to make overloaded constructors more efficient
• You can reduce the amount of repeated code and make it less error-prone
by calling one constructor version from the others
• Use the this reference from one constructor version to call another version
• You cannot call this() from other methods in a class, only from
constructors
• If you call this() from a constructor, it must be the first statement
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.8 Using static fields (1 of 2)
static fields
• Methods you create to use with objects are static
• Most methods you create within a class from which objects will be
instantiated are nonstatic
• Static methods are called class methods because they don’t have an object
associated
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.8 Using static fields (2 of 2)
Class variables
• Variables shared by every instantiation of a class
• There is only one copy of each static class variable per class
Using constant fields
• Sometimes data in a class should be constant
• For a static class, all objects share a single memory location for a field
• A nonstatic final field’s value can be assigned a value in a constructor
• A static final field’s value must be set at declaration
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.9 Using Imported, Prewritten Constants and
Methods (1 of 4)
Library of classes
• A package or folder that groups like prewritten classes together
• java.lang package contains fundamental classes and is implicitly
imported into every program you write
• All other Java packages are optional classes and must be explicitly named
within a program
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.9 Using Imported, Prewritten Constants and
Methods (2 of 4)
math class
• The class java.lang.Math contains constants and methods to preform
mathematical functions
• PI is a commonly used Math class constant
• PI is public, final, static, and double
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.9 Using Imported, Prewritten Constants and
Methods (3 of 4)
Importing Classes
• To use a prewritten class use one of three methods
• Use the entire path with the class name
• Import the class
• Import the package that contains the class using a wildcard symbol (*)
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.9 Using Imported, Prewritten Constants and
Methods (4 of 4)
Using the LocalDate Class
• Use statements that use the static methods now() and of()
• Don’t use the new operator and class a constructor when creating
LocalDate objects
• Specific data field values can retrieve an integer using getYear(),
getMonthValue(), and getDayOfMonth() methods
• Enumeration is a data type that consists of a list of values: getMonth(),
and getDayOfWeek()
• Used in personnel, inventory, and billing systems
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.10 Understanding Composition and Nested
Classes
Composition (1 of 2)
• The relationship between classes when an object of one class is a data field
within another class
• Need to supply values for the contained object if it has no default constructor
• A “has-a relationship” is one in which one class has an instance of another
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.10 Understanding Composition and Nested
Classes
Nested Classes (2 of 2)
• A class withing another class
• The containing class is the top-level class
• static member classes have access to all static methods of the top-level
class
• Nonstatic member classes (inner classes) require an instance and have
access to all data and methods of the top-level class
• Local classes are local to a block
• Anonymous classes have no identifier
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 4.4: Think, Pair, Share
1. Form pairs or groups of 2 to 4 class members.
2. Decide on an application that you will create. You want to create an object
that is an instance of a class. What will you do, and what steps should you
take?
3. You need to include a this reference. What will this help you do?
4. Determine an example of a constant field for your program.
5. Explain what calculation you might use from the Math class.
6. Write code for a portion of your program that will include a nested class.
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 4.4: Think, Pair, Share Debrief
• What object did you create as an instance of a class? What steps did you
take?
• What did you use the this reference for? Why?
• For what purpose did you use a constant field?
• What calculation did you select from the Math class for your program. What
will this calculation provide you with?
• What are the steps that contain a nested class? What is the benefit of
nesting?
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Don’t Do It
• Don’t think that default constructor means only the automatically supplied
version.
• Don’t think that a class’s methods must accept its own fields’ values as
parameters or return values to its own fields.
• Don’t forget to write a default constructor for a class that has other
constructors if you want to be able to instantiate objects without using
arguments.
• Don’t assume that a wildcard in an import statement works like a DOS or
UNIX wildcard. The wildcard works only with specific packages and does not
import embedded packages.
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Self-Assessment
1. What is an is-a relationship?
2. What is the purpose of information hiding?
3. What values are provided in a default constructor?
4. How can you use a this reference to make overloaded constructors more
efficient?
5. How do you use a prewritten class? Why might you use one?
6. What are the four types of nested classes?
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Summary
Click the link to review the objectives for this presentation.
Link to Objectives
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.