Title: Java Programming, Third Edition
1Java Programming, Third Edition
- Chapter Four
- Advanced Object Concepts
2Objectives
- Understand blocks and scope
- Overload a method
- Learn about ambiguity
- Send arguments to constructors
- Overload constructors
3Objectives (continued)
- Learn about the this reference
- Use static variables
- Work with constants
- Use automatically imported, prewritten constants
and methods - Use the explicitly imported prewritten class
GregorianCalendar
4Understanding Blocks and Scope
- Block code between pair of curly braces
- Nesting placing one code block within another
- Example methodWithNestedBlocks ( )
- Outside block begins after method declaration
- Outside block ends with method
- Inside (nested) block within second pair of
braces - Scope portion of a program where variable known
- Begins at declaration, ends when code block ends
5(No Transcript)
6(No Transcript)
7Understanding Blocks and Scope (continued)
- methodWithInvalidStatements ( ) shows errors
- Variable names used prior to declaration
- Variable referenced outside block of declaration
- Declaring variables with the same name
- Allowed in non-overlapping blocks
- Not allowed in the same block
- Examples twoDeclarations ( ) and
invalidRedeclarationMethod ( )
8(No Transcript)
9(No Transcript)
10(No Transcript)
11Understanding Blocks and Scope (continued)
- Method variables assigned separate storage
- Locals hide external variables with same name
- Overriding process of resolving name conflicts
- Example OverridingVariable class
- int aNumber declared in main ( )
- distinct int variables named aNumber in methods
- Method variables override main ( ) variable
12(No Transcript)
13(No Transcript)
14Understanding Blocks and Scope (continued)
- Method variables also hide instance variables
- Example Employee class
- Two attributes
- int empNum
- double empPayRate
- Two methods (second hides instance variables)
- methodThatUsesInstanceVariables ( )
- methodThatUsesLocalVariables ( )
15(No Transcript)
16(No Transcript)
17(No Transcript)
18Overloading a Method
- Overloading one name given diverse meanings
- Purpose refer to similar operations by one name
- Method overloading
- Multiple methods with one name
- Distinguished by type and/or number of arguments
- Example two versions of calculateInterest ( )
- void calculateInterest (double bal, double rate)
- void calculateInterest (double bal, int rate)
19(No Transcript)
20(No Transcript)
21Learning About Ambiguity
- Methods with distinct names are unambiguous
- Two types of unambiguous method calls
- Argument types match formal parameter types
- Argument types do not match parameter types
- If argument higher, application does not compile
- If argument lower, argument promoted
- Example CallSimpMeth class
- Includes void simpMeth (double d)
- Calls simpMeth ( ) twice, with double and int
types
22(No Transcript)
23(No Transcript)
24(No Transcript)
25Learning About Ambiguity (continued)
- Methods with the same name may be unambiguous
- Distinct argument lists (vary by type and/or
number) - Arguments can be matched to only one method
- Ambiguous situations
- Arguments can be matched to more than one method
- Compiler cannot determine appropriate method
- Program does not compile
26Learning About Ambiguity (continued)
- Headers which may lead to ambiguous situations
- public static void calculateInterest (int bal,
double rate) - public static void calculateInterest (double bal,
int rate) - Safe calls argument types match formal
parameters' - Unsafe call calculateInterest (300, 6)
- By promotion, arguments pass to either method
- Compiler has no criteria for selection, issues
error - Illegal methods identical names and argument
lists, but different return types
27Sending Arguments to Constructors
- Automatic default constructor
- Provided by compiler in absence of explicit
method - Initializes data fields with default values
- Programmers may explicitly code constructors
- Provides control over initialization of object
- May contain arguments
- Examples two constructors for Employee class
- Explicit constructor suppresses Java default
version
28(No Transcript)
29(No Transcript)
30Overloading Constructors
- Constructors may be overloaded (like other
methods) - Compiler distinguishes constructors by argument
list - Examines number and type of arguments
- Example Employee Class
- Explicit default constructor (no arguments)
- Parameterized constructor (one integer argument)
31(No Transcript)
32Learning about the this Reference
- Object's instance variables allocated separate
storage - Reference object's memory address
- Instance methods shared by all class objects
- this reference implicitly passed to each instance
method - this points to first cell of first instance
variable - this may be explicitly used in a program
33(No Transcript)
34Using Static Variables
- this reference associated with an object
- Class methods and variables allocated static
storage - Static members generally available to entire
class - Static members not associated with object
- this reference not passed to class (static)
methods - Example TestPlayer class
- Call to BaseballPlayer class
- BaseballPlayer's static count independent of
object
35(No Transcript)
36(No Transcript)
37(No Transcript)
38Working with Constants
- Constant variable data field stays fixed
- Literal constant hard code such as 7 or 3.5
- Symbolic constant named such as SCHOOL_ID
- Using symbolic constants
- final keyword final precedes data type in
declaration - Convention dictates constant name in uppercase
letters - Ex private static final int SCHOOL_ID 12345
- Benefits of symbolic constants
- Program easy to read, maintain, protect
39(No Transcript)
40Using Automatically Imported, Prewritten
Constants and Methods
- Java creators developed nearly 500 classes
- Package library of classes (convenient grouping)
- Two types of class packages
- Fundamental basic classes automatically imported
- Example java.lang
- Optional must be explicitly imported
- Examples java.util and java.swing
41Using Automatically Imported, Prewritten
Constants and Methods (continued)
- The fundamental class Java.lang.Math
- Contains mathematical constants and methods
- Example constant PI
- Example method Math.max ( )
- All constants and methods are static
- Two options for referencing constants and methods
- Specify full package path java.lang.Math.PI
- Specify Math class and object Math.PI
42(No Transcript)
43Using an Explicitly Prewritten Imported Class and
its Methods
- Three ways to reference prewritten optional
classes - Use the entire path with the class name or
- Import the class or
- Import package containing class you are using
- Wildcard symbol () represents any set of
symbols - Import package of classes using wildcard
- Example import java.util.
- Represents all the classes in java.util package
44Using an Explicitly Prewritten Imported Class and
its Methods (continued)
- GregorianCalendar class models Gregorian calendar
- Located in java.util package
- Inherits from abstract Calendar class
- GregorianCalendar contains seven constructors
- Default calendar object sets default date, time,
locale - Overloaded constructors specify date values
- Constructing and using GregorianCalendar object
- GregorianCalendar calendar new
GregorianCalendar( ) - int nowYear now.get(Calendar.YEAR)
45(No Transcript)
46(No Transcript)
47(No Transcript)
48Summary
- Block section of code contained within braces
() - Scope portion of program where variable visible
- Same name declarations in non-overlapping blocks
- Local variables hide others in class with same
name - Overloaded method shares name, has unique arg list
49Summary (continued)
- Ambiguous call compiler cannot select method
- Constructors may be overloaded
- Constructors may be passed arguments
- Use of explicit constructor dismisses Java
default - Static variables and methods shared by class
50Summary (continued)
- Instance methods stored once for class
- Instance variables stored separately for each
object - this reference implicitly passed to instance
method - Declaration of symbolic constants requires final
- Prewritten classes implicitly or explicitly
imported