Title: Java Programming, Third Edition
1Java Programming, Third Edition
- Chapter Three
- Using Methods, Classes, and Objects
2Objectives
- Create methods with no arguments, a single
argument, and multiple arguments - Create methods that return values
- Learn about class concepts
- Create a class
3Objectives (continued)
- Create instance methods in a class
- Declare objects and use their methods
- Organize classes
- Begin to understand how to use constructors
4Creating Methods with Zero, One, and Multiple
Arguments
- Method series of statements that carry out a
task - Classes may contain unlimited number of methods
- Methods must include
- A declaration
- An opening curly brace
- A body
- A closing brace
- Review and revise First class
5(No Transcript)
6Creating Methods with Zero, One, and Multiple
Arguments (continued)
- Method declarations must contain
- Optional access modifiers
- The return type for the method
- The method name
- An opening parenthesis
- An optional list of method arguments
- A closing parenthesis
7Creating Methods with Zero, One, and Multiple
Arguments (continued)
- Access modifiers for a method can be
- public, private, protected, package (default)
- Static storage modifier method independent of
object - Implement nameAndAddress ( ) method
- Modification public and static
- Logic calls println ( ) three times, passes
three strings - Place definition within class, not within other
method
8(No Transcript)
9(No Transcript)
10Creating Methods with Zero, One, and Multiple
Arguments (continued)
- Using nameAndAddress ( ) in revised First class
- Write method name into main ( )
- Method called during program execution
- Output four lines of text
- println ( ) called indirectly and directly
- Using full name, First.nameAndAddress ( )
- Necessary when method used outside of First class
- Not needed for use within class (First, in this
case)
11(No Transcript)
12(No Transcript)
13Creating Methods that Require a Single Argument
- Implementation hiding
- Method interface known to user
- Method body is a black box
- Arguments communications to a method
- Each argument must specify data type, local name
- Code arguments into predictRaise ( ) method
14Creating Methods that Require a Single Argument
(continued)
- Method declaration
- public void predictRaise (double moneyAmount)
- Method body
- Declare local variable (double newAmount)
- Local variable has local scope (not known to
caller) - Calculate adjusted salary using argument
- Assign result to newAmount
- Output new salary using printlin ( )
15(No Transcript)
16Creating Methods that Require a Single Argument
(continued)
- Call syntax
- Use method name
- Place constant or variable name between ( )
- Variable name in call may differ from header
- Examples
- predictRaise(400.00)
- predictRaise (mySalary)
- predictRaise(moneyAmount)
17(No Transcript)
18(No Transcript)
19Creating Methods that Require Multiple Arguments
- Methods can require more than one argument
- Pass multiple arguments by
- Listing the arguments in the call to the method
- Separating them with commas
- Each argument specified by data type and name
- Example add argument to predictRaise ( )
- Rate was previously hard-coded in definition
- New rate variable (double) added to argument list
20(No Transcript)
21Creating Methods that Require Multiple Arguments
(continued)
- Passed arguments match header in type, number,
order - Another example computeCommission ( )
- Declaration
- public static void computeCommission(int value,
-
double rate, char vehicle) - Calls using literals and variable names
- computeCommission(40000, 0.10, L)
- computeCommission(value, commRate, vType)
22(No Transcript)
23(No Transcript)
24Creating Methods that Return Values
- void methods return no values
- Example public static void nameAndAddress( )
- Value-returning type also known as methods type
- Two new design elements
- Include data type in header
- Include return statement in method body (last
line) - Java restricts return to one type (primitive or
class) - Example predictRaise ( ) redesigned to return
double
25(No Transcript)
26Learning about Class Concepts
- Basic perspective in object-oriented programming
- Everything is an object
- Every object is a member of a class
- Class-object relationship
- Blueprint to concrete instance (instantiation)
- Template ltobjectgt is a ltclass_typegt
- Example "is a" relationships
- My oak desk (object) is a Desk (class)
- Moby (instance) is a Fish (class)
27Learning about Class Concepts (continued)
- Usefulness of class concept
- Objects inherit attributes from classes
- Objects have predictable attributes as class
members - Intuitive objects send (or receive) requests
(messages) - Two chief ways to utilize classes
- Run as application
- Instantiate objects from class
- Prewritten classes may be called by class client
(user)
28Creating a Class
- Overview of class creation
- Assign a name to the class
- Determine data and methods required by class
- Writing a three-part class header
- An optional access modifier (public, final,
abstract) - The keyword class
- User-selected legal identifier for class name
- Public access least restrictive and inheritance
friendly - Illustration declare new Employee class
29(No Transcript)
30Creating a Class (continued)
- Instance variables (data fields or fields)
attributes - Four access levels for member data (and methods)
- private, public, protected, package (default)
- Private access promotes information hiding
- Highest level of security
- Data member cannot be directly accessed
- Public method of same class interface to data
fields - Illustration declare variable in Employee class
31(No Transcript)
32Creating Instance Methods in a Class
- Static (class) method called independently of
object - Instance methods
- Require an instantiated object
- Used to interface with data members
- Implement two public instance methods in Employee
- getEmpNum ( ) accessor returns current attribute
value - setEmpNum ( ) mutator sets attribute with passed
arg
33(No Transcript)
34(No Transcript)
35(No Transcript)
36Declaring Objects
- To declare an object
- Supply a type and an identifier
- Allocate computer memory for the object
- Use the new operator
- Example using condensed syntax
- Employee someEmployee new Employee( )
- Employee ( ) method is a constructor
- Employee objects used in DeclareTwoEmployees class
37(No Transcript)
38(No Transcript)
39Understanding Data Hiding
- Data hiding
- Key concept in object-oriented programming
- Public method interfaces with private data
- DeclareTwoEmployees indirectly accesses private
data - Access violation clerk.empNum 789
- Get and set methods provide access channel to
data - Get and set provide framework for maintenance
- May be recoded for efficiency and/or more
protection
40Organizing Classes
- Conventions regarding instance variables
- Place data fields in logical order at class start
- Use a unique, descriptive identifier for
attributes - Example empNum
- Conventions regarding instance methods
- Place definitions after listing of data members
- Use descriptive identifier in method name
- Group by one or more criteria (alphabetical
order) - Note method prototypes not required in Java
41(No Transcript)
42(No Transcript)
43Using Constructor Methods
- Constructor methods instantiate new class objects
- Must have same name as class
- May not have a return type, not even void
- May include arguments in interface
- Explicit constructor for Employee set salary to
300.00 - Call Employee chauffer new Employee( )
- If explicit constructor absent, Java constructs
default
44(No Transcript)
45Summary
- Methods may have 0 or more arguments
- Methods are modular
- Methods have a header and body
- Java does not require method prototypes
- Method access levels private, public, protected
46Summary (continued)
- Static methods are independent of an object
- Instance methods require an object
- Methods may return primitive or class data types
- Methods sending data need return statement and
type - Void methods have no return type
47Summary (continued)
- ltobjectgt is a class_type classes are object
blueprints - Public class access permits inheritance
- Encapsulation hides method implementation and
data - Instance methods interface with private data
- Explicit (or implicit) constructor establishes
object