0% found this document useful (0 votes)
2 views55 pages

Java Methods, Classes, and Objects Guide

Chapter 3 of the Java Programming document focuses on using methods, classes, and objects, detailing how to define and call methods, including adding parameters and return values. It explains the structure of a method, the importance of access specifiers, and the concept of classes and objects, including instantiation and data encapsulation. The chapter also covers creating classes, instance methods, and the differences between static and nonstatic elements.

Uploaded by

moekadi.molefe
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views55 pages

Java Methods, Classes, and Objects Guide

Chapter 3 of the Java Programming document focuses on using methods, classes, and objects, detailing how to define and call methods, including adding parameters and return values. It explains the structure of a method, the importance of access specifiers, and the concept of classes and objects, including instantiation and data encapsulation. The chapter also covers creating classes, instance methods, and the differences between static and nonstatic elements.

Uploaded by

moekadi.molefe
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

JAVA PROGRAMMING

CHAPTER 3

Using Methods, Classes,


and Objects
OBJECTIVES

• Describe method calls and placement

• Identify the parts of a method

• Add parameters to methods

• Create methods that return values

• Learn about classes and objects

• Create a class

• Create instance methods in a class

• Declare objects and use their methods

• Create constructors

• Appreciate classes as data types

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Method
• A program module
• Contains a series of statements
• Carries out a task
UNDERSTANDIN
G METHOD Execute a method
CALLS AND • Invoke or call from another method
PLACEMENT (1 Calling method (client
OF 4) method)
• Makes a method call

Called method
• Invoked by a calling method

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
UNDERSTANDING METHOD CALLS
AND PLACEMENT (2 OF 4)

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted
in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
main() method
executes
UNDERSTANDIN automatically
G METHOD
CALLS AND
PLACEMENT (3
OF 4) Other methods
are called as
needed

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
UNDERSTANDING METHOD
CALLS AND PLACEMENT (4 OF 4)

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted
in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
UNDERSTANDING
METHOD
CONSTRUCTION (1 OF 3)

• A method must include:


• Method header
• Also called a declaration
• Method body
• Between a pair of curly braces
• Contains the statements that
carry out the work
• Also called implementation

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in
a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
UNDERSTANDING METHOD
CONSTRUCTION (2 OF 3)

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted
in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
UNDERSTANDING
METHOD
CONSTRUCTION (3 OF 3)

• The method header contains:


• Optional access specifiers
• A return type
• An identifier
• Parentheses
• Might contain data to be sent to
the method

• Place the entire method within


the class that will use it
• Not within any other method

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in
a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
ACCESS SPECIFIERS (1 OF 2)

Can be public,
public access
private,
allows use by
protected , or
any other class
package

Methods most Also called


commonly use access
public access modifiers

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
ACCESS SPECIFIERS (2 OF 2)

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted
in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
RETURN TYPE (1 OF 2)

• Describes the type of data the method sends back to the


calling method
• If no data is returned to the method, the return value is
void

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
RETURN TYPE (2 OF 2)

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted
in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
METHOD NAME (1 OF 2)

• Can be any legal identifier


• Must be one word
• No embedded spaces
• Cannot be a Java keyword

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
METHOD NAME (2 OF 2)

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted
in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
PARENTHESES (1 OF 2)

• Every method header contains a set of parentheses that


follow the identifier
• May contain data to be sent to the method
• Fully qualified identifier
• A complete name that includes the class

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
PARENTHESES (2 OF 2)

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted
in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
ADDING PARAMETERS
TO METHODS

• Arguments
• Data items you use in a call to a method

• Parameters
• Data items received by the method

• Implementation hiding
• Encapsulation of method details within a
class
• The calling method needs to understand
only the interface to the called method
• Interface
• The only part of a method that the
client sees or with which it interacts

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in
a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
CREATING A METHOD
THAT RECEIVES A SINGLE
PARAMETER (1 OF 4)

• Define the following:


• Optional access specifiers
• Return type for the method
• Method name
• Parameter type
• Local name for the parameter

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in
a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
CREATING A METHOD THAT RECEIVES A
SINGLE PARAMETER (2 OF 4)

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted
in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
CREATING A METHOD
THAT RECEIVES A SINGLE
PARAMETER (3 OF 4)

• Local variable
• Known only within the
boundaries of the method
• Each time the method executes:
• The variable is redeclared
• A new memory location large
enough to hold the type is set up
and named

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in
a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
CREATING A
METHOD
THAT
RECEIVES A
SINGLE
PARAMETER
(4 OF 4)

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted
in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
• A method can require more than one
parameter
• List the arguments within the call to the
method
CREATING A • Separate with commas
METHOD • Call a method
THAT • Arguments sent to the method must
REQUIRES match the parameters listed in the
MULTIPLE method declaration by:
PARAMETERS • Number

(1 OF 2) • Type
• Method signature
• Method name
• Number, types, and order of the
arguments

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
CREATING A METHOD THAT REQUIRES
MULTIPLE PARAMETERS (2 OF 2)

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted
in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
CREATING METHODS
THAT RETURN
VALUES (1 OF 3)
• return statement
• Causes a value to be sent from the
called method back to the calling
method
• The return type can be any type used in Java
• Primitive types
• Class types
• void
• Returns nothing
• Method’s type
• A method’s return type

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in
a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
CREATING METHODS THAT
RETURN VALUES (2 OF 3)
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted
in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
CREATING METHODS
THAT RETURN
VALUES (3 OF 3)
• Unreachable statements
(dead code)
• Logical flow leaves the method at
the return statement
• Can never execute
• Causes a compiler error

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in
a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
CHAINING METHOD
CALLS (1 OF 2)

• Any method might call any


number of other methods
• Method acts as a black box
• Do not need to know how it
works
• Just call and use the result

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in
a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
CHAINING METHOD CALLS (2 OF
2)

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted
in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
LEARNING ABOUT CLASSES
AND OBJECTS
• Every object is a member of a class

• Is-a relationships
• An object “is a” concrete example of the class
• The zoo’s shark “is a” Fish

• Instantiation
• Shark is an instantiation of the Fish class

• Reusability

• Methods are often called upon to return a piece of information to the source of the
request

• Class client or class user


• An application or a class that instantiates objects of another prewritten class

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
CREATING A CLASS (1 OF 4)

• Assign a name to the class


• Determine what data and methods will be part of the class
• Create a class header with three parts:
• An optional access modifier
• The keyword class
• Any legal identifier for the name of the class

• public class
• Accessible by all objects

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
CREATING A CLASS (2 OF 4)

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted
in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
CREATING A CLASS (3 OF 4)

• Extended
• To be used as a basis for any other class

• Data fields
• Variables declared within a class but outside of any method

• Instance variables
• Nonstatic fields given to each object

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
CREATING A CLASS (4 OF 4)

Private access for fields


• No other classes can access the field’s values
• Only methods of the same class are allowed to use private
variables

Information hiding

Most class methods are public

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
CREATING INSTANCE METHODS
IN A CLASS (1 OF 4)
• Classes contain methods
• Mutator methods
• Set or change field values
• Accessor methods
• Retrieve values
• Nonstatic methods
• Instance methods
• “Belong” to objects
• Typically declare nonstatic data fields
• static class variables are not instance variables
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Table 3-1 Comparison of static and nonstatic

Static Nonstatic

In Java, static is a keyword. It also can be There is no keyword for nonstatic items. When you do not explicitly
used as an adjective. declare a field or method to be static, it is 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 called 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 When you use a nonstatic field or method, you must use an object;
object; for example: [Link](); for example: [Link]();

When you create a class with a static field and instantiate 100 When you create a class with a nonstatic field and instantiate 100
objects, only one copy of that field exists in memory. objects, then 100 copies of that field exist in memory.

When you create a static method in a class and instantiate 100 When you create a nonstatic method in a class and instantiate 100
objects, only one copy of the method exists in memory and the objects, only one copy of the method exists in memory, but the
method does not receive a this reference. method receives a this reference that contains the address of the
object currently using it.
Static class variables are not instance variables. The system Instance fields and methods are nonstatic. The system allocates a
allocates memory to hold class variables once per class, no matter separate memory location for each nonstatic field in each instance.
how many instances of the class you instantiate. The system
allocates memory for class variables the first time it encounters a
class, and every instance of a class shares the same copy of any
static class variables.

CREATING INSTANCE METHODS IN A


CLASS (2 OF 4)

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted
in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
CREATING
INSTANCE
METHODS
IN A CLASS
(3 OF 4)

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted
in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
CREATING
INSTANCE
METHODS IN A
CLASS (4 OF 4)

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted
in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Place data fields in logical
order
At the beginning of a List the fields
class vertically

ORGANIZING
CLASSES (1 OF
4)
Data fields and methods may
be placed in any order within a
class
Names and data
types can be seen
It’s common to list
before reading the
all data fields first
methods that use
the data fields

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
ORGANIZING CLASSES (2 OF 4)

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted
in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
ORGANIZING CLASSES (3 OF 4)

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted
in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
ORGANIZING CLASSES (4 OF 4)
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted
in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
DECLARING OBJECTS
AND USING THEIR
METHODS (1 OF 4)
• Declaring a class does not create any actual
objects
• To create an instance of a class:
• Supply a type and an identifier
• Allocate computer memory for the
object
• Use the new operator
Employee someEmployee;
someEmployee = new Employee();
or
Employee someEmployee = new
Employee();

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in
a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
DECLARING OBJECTS AND USING
THEIR METHODS (2 OF 4)

Reference to the object


The name for a memory address where the object is held

Constructor method
The name of the
constructor is always the
A method that creates and You can write your own Java writes a constructor
same as the name of the
initializes class objects constructor methods when you don’t write one
class whose objects it
constructs

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
DECLARIN
• After an object is instantiated, its
G OBJECTS
methods can be accessed using:
AND USING
• The object’s identifier
THEIR
• A dot
METHODS
• A method call
(3 OF 4)

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
DECLARING OBJECTS AND USING
THEIR METHODS (4 OF 4)

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted
in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
UNDERSTANDING DATA HIDING

• Data hiding using encapsulation


• Data fields are usually private
• The client application accesses them only through public
interfaces

• set method
• Controls the data values used to set a variable

• get method
• Controls how a value is retrieved

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
AN INTRODUCTION TO USING
CONSTRUCTORS (1 OF 4)
Employee chauffeur = new Employee();
• Actually a calling method named Employee()

• Default constructors
• Require no arguments
• Created automatically by a Java compiler
• For any class
• Whenever you do not write a constructor

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
AN INTRODUCTION TO USING
CONSTRUCTORS (2 OF 4)
• The default constructor provides specific initial values to
an object’s data fields
• Numeric fields
• Set to 0 (zero)
• Character fields
• Set to Unicode ‘\u0000’
• Boolean fields
• Set to false
• Nonprimitive object fields
• Set to null

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
AN INTRODUCTION TO USING
CONSTRUCTORS (3 OF 4)
• A constructor method:
• Must have the same name as the class it constructs
• Cannot have a return type
• public access modifier

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
AN
INTRODUCTION
TO USING
CONSTRUCTOR
S (4 OF 4)

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted
in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
UNDERSTANDING THAT
CLASSES ARE DATA TYPES
• Classes you create become data types
• Often referred to as abstract data types (ADTs)
• Implementation is hidden and accessed through public
methods
• Programmer-defined data type
• Not built into the language

• Declare an object from one of your classes


• Provide the type and identifier

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
DON’T DO IT

• Don’t place a semicolon at the end of a method header


• Don’t think “default constructor” means only the
automatically supplied constructor
• Don’t think that a class’s methods must:
• Accept its own fields’ values as parameters
• Return values to its own fields

• Don’t create a class method that has a parameter with


the same identifier as a class field

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
SUMMARY (1 OF 2)

• Method
• A series of statements that carry out a task
• A declaration includes the parameter type and local name for
a parameter
• You can pass multiple arguments to methods
• Has a return type

• Class objects
• Have attributes and methods associated with them

• Instantiate objects that are members of a class


© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
SUMMARY (2 OF 2)

• Constructor
• A method establishes an object and provides specific initial
values for an object’s data fields

• Everything is an object
• Every object is a member of a more general class

• Implementation hiding, or encapsulation


• private data fields
• public access methods

© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

You might also like