You are on page 1of 47

Java Programming Fifth Edition

Chapter 4 More Object Concepts

Objectives
Learn about blocks and scope Overload a method Learn about ambiguity Send arguments to constructors Overload constructors Learn about the this reference

Java Programming, Fifth Edition

Objectives (continued)
Use static variables Use constant fields Use automatically imported, prewritten constants and methods Use the explicitly imported prewritten class GregorianCalendar

Java Programming, Fifth Edition

Objectives (continued)
Learn about composition Learn about nested and inner classes

Java Programming, Fifth Edition

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

Java Programming, Fifth Edition

Understanding Blocks and Scope (continued)

Java Programming, Fifth Edition

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

Java Programming, Fifth Edition

Understanding Blocks and Scope (continued)


Redeclare variable
Cannot declare same variable name more than once within block Illegal action

Java Programming, Fifth Edition

Understanding Blocks and Scope (continued)

Java Programming, Fifth Edition

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

Java Programming, Fifth Edition

10

Understanding Blocks and Scope (continued)

Java Programming, Fifth Edition

11

Understanding Blocks and Scope (continued)

Java Programming, Fifth Edition

12

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
Java Programming, Fifth Edition 13

Overloading a Method (continued)

Java Programming, Fifth Edition

14

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

Java Programming, Fifth Edition

15

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

Java Programming, Fifth Edition

16

Learning About Ambiguity (continued)

Java Programming, Fifth Edition

17

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

Java Programming, Fifth Edition

18

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

Java Programming, Fifth Edition

19

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

Java Programming, Fifth Edition

20

Overloading Constructors (continued)

Java Programming, Fifth Edition

21

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

Java Programming, Fifth Edition

22

Learning About the this Reference (continued)


Reference
Objects memory address Implicit
Automatically understood without actually being written

Java Programming, Fifth Edition

23

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

Java Programming, Fifth Edition

24

Learning About the this Reference (continued)

Java Programming, Fifth Edition

25

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

Java Programming, Fifth Edition

26

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

Java Programming, Fifth Edition

28

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 of static class variable per class

Java Programming, Fifth Edition

29

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

Using Constant Fields (continued)

Java Programming, Fifth Edition

31

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

Java Programming, Fifth Edition

32

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

Java Programming, Fifth Edition

33

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 for Math class private

Java Programming, Fifth Edition

34

Java Programming, Fifth Edition

35

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

Java Programming, Fifth Edition

36

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

Java Programming, Fifth Edition

37

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

Java Programming, Fifth Edition

39

Java Programming, Fifth Edition

40

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

Understanding Composition (continued)

Java Programming, Fifth Edition

42

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

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

Java Programming, Fifth Edition

44

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 different return 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

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
Java Programming, Fifth Edition 46

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

Java Programming, Fifth Edition

47

You might also like