Upload
ed-win
View
213
Download
0
Embed Size (px)
Citation preview
8/4/2019 9780324599510_PPT_ch04
1/47
Java Programming
Fifth Edition
Chapter 4More Object Concepts
8/4/2019 9780324599510_PPT_ch04
2/47
Java Programming, Fifth Edition 2
Objectives
Learn about blocks and scope
Overload a method
Learn about ambiguity
Send arguments to constructors
Overload constructors
Learn about the this reference
8/4/2019 9780324599510_PPT_ch04
3/47
Java Programming, Fifth Edition 3
Objectives (continued)
Use static variables
Use constant fields
Use automatically imported, prewritten constants
and methods
Use the explicitly imported prewritten classGregorianCalendar
8/4/2019 9780324599510_PPT_ch04
4/47
Java Programming, Fifth Edition 4
Objectives (continued)
Learn about composition
Learn about nested and inner classes
8/4/2019 9780324599510_PPT_ch04
5/47
Java Programming, Fifth Edition 5
Understanding Blocks and Scope
Blocks
Use opening and closing curly braces
Can exist entirely within another block or entirely
outside and separate from another block
Cannot overlap
Types
Outside or outer blocks
Inside or inner blocks
Nested
8/4/2019 9780324599510_PPT_ch04
6/47
Understanding Blocks and Scope
(continued)
Java Programming, Fifth Edition 6
8/4/2019 9780324599510_PPT_ch04
7/47
Java Programming, Fifth Edition 7
Understanding Blocks and Scope
(continued) Scope
Portion of program within which you can refer to a
variable
Comes into scope
Variable comes into existence
Goes out of scope
Variable ceases to exist
8/4/2019 9780324599510_PPT_ch04
8/47
Java Programming, Fifth Edition 8
Understanding Blocks and Scope
(continued) Redeclare variable
Cannot declare same variable name more than once
within block
Illegal action
8/4/2019 9780324599510_PPT_ch04
9/47
Understanding Blocks and Scope
(continued)
Java Programming, Fifth Edition 9
8/4/2019 9780324599510_PPT_ch04
10/47
Java Programming, Fifth Edition 10
Understanding Blocks and Scope
(continued) Override
Variables name within method in which it is declared
Takes precedence over any other variable with same
name in another method
Locally declared variables
Always mask or hide other variables with same name
elsewhere in class
8/4/2019 9780324599510_PPT_ch04
11/47
Understanding Blocks and Scope
(continued)
Java Programming, Fifth Edition 11
8/4/2019 9780324599510_PPT_ch04
12/47
Understanding Blocks and Scope
(continued)
Java Programming, Fifth Edition 12
8/4/2019 9780324599510_PPT_ch04
13/47
Java Programming, Fifth Edition 13
Overloading a Method
Overloading
Using one term to indicate diverse meanings
Writing multiple methods with same name but with
different arguments
Compiler understands meaning based on arguments
used with method
Convenience for programmers to use one
reasonable name For tasks that are functionally identical
Except for argument types
8/4/2019 9780324599510_PPT_ch04
14/47
Overloading a Method (continued)
Java Programming, Fifth Edition 14
8/4/2019 9780324599510_PPT_ch04
15/47
Java Programming, Fifth Edition 15
Learning About Ambiguity
If application contains just one version of method
Call method using parameter of correct data type
Or one that can be promoted to correct data type
Ambiguous situation
When methods overloaded
May create situation in which compiler cannot
determine which method to use
8/4/2019 9780324599510_PPT_ch04
16/47
Java Programming, Fifth Edition 16
Learning About Ambiguity (continued)
Overload methods
Correctly provide different argument lists for
methods with same name
Illegal methods
Methods with identical names that have identical
argument lists but different return types
8/4/2019 9780324599510_PPT_ch04
17/47
Learning About Ambiguity (continued)
Java Programming, Fifth Edition 17
8/4/2019 9780324599510_PPT_ch04
18/47
Java Programming, Fifth Edition 18
Sending Arguments to Constructors
Java automatically provides constructor method
When class-created default constructors do not
require parameters
Write your own constructor method
Ensures that fields within classes are initialized to
appropriate default values
Constructors can receive parameters
Used for initialization purposes
8/4/2019 9780324599510_PPT_ch04
19/47
Java Programming, Fifth Edition 19
Sending Arguments to Constructors
(continued) Write constructor for class
No longer receive automatically written default
constructor
If classs only constructor requires parameter
Must provide argument for every object of class
8/4/2019 9780324599510_PPT_ch04
20/47
Java Programming, Fifth Edition 20
Overloading Constructors
Use constructor parameters
To initialize field values
Or any other purpose
If constructor parameter lists differ
No ambiguity about which constructor method to call
8/4/2019 9780324599510_PPT_ch04
21/47
Overloading Constructors (continued)
Java Programming, Fifth Edition 21
8/4/2019 9780324599510_PPT_ch04
22/47
Java Programming, Fifth Edition 22
Learning About the this Reference
Instantiate object from class
Memory reserved for each instance field in class
Not necessary to store separate copy of each
variable and method for each instantiation of class
In Java
One copy of each method in class stored
All instantiated objects can use one copy
8/4/2019 9780324599510_PPT_ch04
23/47
Java Programming, Fifth Edition 23
Learning About the this Reference
(continued) Reference
Objects memory address
Implicit
Automatically understood without actually being
written
8/4/2019 9780324599510_PPT_ch04
24/47
Java Programming, Fifth Edition 24
Learning About the this Reference
(continued) this reference
Reference to object
Passed to any objects nonstatic class method
Reserved word in Java
Dont need to use this reference in methods you
write
In most situations
8/4/2019 9780324599510_PPT_ch04
25/47
Learning About the this Reference
(continued)
Java Programming, Fifth Edition 25
8/4/2019 9780324599510_PPT_ch04
26/47
Java Programming, Fifth Edition 26
Learning About the this Reference
(continued) this reference (continued)
Implicitly received by instance methods
Use to make classes work correctly
When used with field name in class method
Reference to class field
Instead of to local variable declared within method
8/4/2019 9780324599510_PPT_ch04
27/47
Using the this Reference to Make
Overloaded Constructors
More Efficient Avoid repetition within constructors
Constructor calls other constructor
this() More efficient and less error-prone
Java Programming, Fifth Edition 27
8/4/2019 9780324599510_PPT_ch04
28/47
Java Programming, Fifth Edition 28
8/4/2019 9780324599510_PPT_ch04
29/47
Java Programming, Fifth Edition 29
Using static Variables
Class methods
Do not have this reference
Have no object associated with them
Class variables
Shared by every instantiation of class
Only one copy ofstatic class variable per class
8/4/2019 9780324599510_PPT_ch04
30/47
Using Constant Fields
Create named constants using keyword final
Make its value unalterable after construction
Can be set in class constructor
After construction cannot change final fields value
Java Programming, Fifth Edition 30
8/4/2019 9780324599510_PPT_ch04
31/47
Using Constant Fields (continued)
Java Programming, Fifth Edition 31
8/4/2019 9780324599510_PPT_ch04
32/47
Java Programming, Fifth Edition 32
Using Automatically Imported,
Prewritten Constants and Methods Many classes commonly used by wide variety of
programmers
Java contains nearly 500 classes
Package or library of classes
Folder provides convenient grouping for classes
Many contain classes available only if explicitly
named within program
Some classes available automatically
8/4/2019 9780324599510_PPT_ch04
33/47
Java Programming, Fifth Edition 33
Using Automatically Imported,
Prewritten Constants and Methods
(continued) Fundamental or basic classes
Implicitly imported into every Java program
java.lang package
Only automatically imported, named package
Optional classes
Must be explicitly named
8/4/2019 9780324599510_PPT_ch04
34/47
Java Programming, Fifth Edition 34
Using Automatically Imported,
Prewritten Constants and Methods
(continued) java.lang.Math class
Contains constants and methods used to perform
common mathematical functions
No need to create instance
Imported automatically
Cannot instantiate objects of type Math
Constructor forMath class private
8/4/2019 9780324599510_PPT_ch04
35/47
Java Programming, Fifth Edition 35
8/4/2019 9780324599510_PPT_ch04
36/47
Java Programming, Fifth Edition 36
Using an Explicitly Imported
Prewritten Class and Its Methods Use prewritten classes
Use entire path with class name
Import class
Import package that contains class
To use import statements
Place before any executing statement in Java file
Prior to import statement
Use blank line or comment line but nothing else
8/4/2019 9780324599510_PPT_ch04
37/47
Java Programming, Fifth Edition 37
Using an Explicitly Imported
Prewritten Class and Its Methods
(continued) Wildcard symbol
Alternative to importing class
Import entire package of classes
Use asterisk
Can be replaced by any set of characters
Represents all classes in package
No disadvantage to importing extra classes
Importing each class by name can be form of
documentation
8/4/2019 9780324599510_PPT_ch04
38/47
Using an Explicitly Imported
Prewritten Class and Its Methods
(continued) GregorianCalendar class
Seven constructors
Default constructor creates calendar object
containing current date and time in default locale
(time zone)
Can also specify date, time, and time zone
get() method
Access data fields
Java Programming, Fifth Edition 38
8/4/2019 9780324599510_PPT_ch04
39/47
Java Programming, Fifth Edition 39
8/4/2019 9780324599510_PPT_ch04
40/47
Java Programming, Fifth Edition 40
8/4/2019 9780324599510_PPT_ch04
41/47
Understanding Composition
Composition
Describes relationship between classes when object
of one class data field is within another class
Called has-a relationship Because one class has an instance of another
Remember to supply values for contained object if
it has no default constructor
Java Programming, Fifth Edition 41
8/4/2019 9780324599510_PPT_ch04
42/47
Understanding Composition
(continued)
Java Programming, Fifth Edition 42
8/4/2019 9780324599510_PPT_ch04
43/47
A Brief Look at Nested and Inner
Classes
Nested classes
Class within another class
Stored together in one file
Nested class types
static member
Nonstatic member
Local classes
Anonymous classes
Java Programming, Fifth Edition 43
8/4/2019 9780324599510_PPT_ch04
44/47
Java Programming, Fifth Edition 44
You Do It
Demonstrating scope
Overloading methods
Creating a constructor that requires an argument
Using an explicitly imported prewritten class
Creating an interactive application with a timer
8/4/2019 9780324599510_PPT_ch04
45/47
Dont Do It
Dont try to use a variable that is out of scope
Dont assume that a constant is still a constant
when passed to a methods parameter
Dont overload methods by giving them differentreturn types
Dont think that default constructor means only the
automatically supplied version
Dont forget to write a default constructor for a
class that has other constructors
Java Programming, Fifth Edition 45
8/4/2019 9780324599510_PPT_ch04
46/47
Java Programming, Fifth Edition 46
Summary
Variables scope
Portion of program in which you can reference
variable
Block Code between a pair of curly braces
Overloading
Writing multiple methods with same name but
different argument lists
Store separate copies of data fields for each object
But just one copy of each method
8/4/2019 9780324599510_PPT_ch04
47/47
Java Programming, Fifth Edition 47
Summary (continued)
static class variables
Shared by every instantiation of a class
Literal constants never change
Prewritten classes
Stored in packages
import statement
Notifies Java program that class names refer to
those within imported class