defining classes and methods chapter 4. object-oriented programming our world consists of objects...

78
Defining Classes and Methods Chapter 4

Upload: duane-johns

Post on 20-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Defining Classes and Methods

Chapter 4

Page 2: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Object-Oriented Programming

• Our world consists of objects (people, trees, cars, cities, airline reservations, etc.).

• Objects can perform actions which effect themselves and other objects in the world.

• Object-oriented programming (OOP) treats a program as a collection of objects that interact by means of actions.

Page 3: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

OOP Design Principles

• OOP adheres to three primary design principles:– encapsulation– polymorphism– inheritance.

Page 4: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Introduction to Encapsulation

• The data and methods associated with any particular class are encapsulated (“put together in a capsule”), but only part of the contents is made accessible.– Encapsulation provides a means of using the

class, but it omits the details of how the class works.

– Encapsulation often is called information hiding.

Page 5: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Accessibility Example

• An automobile consists of several parts and pieces and is capable of doing many useful things.– Awareness of the accelerator pedal, the brake

pedal, and the steering wheel is important to the driver.

– Awareness of the fuel injectors, the automatic braking control system, and the power steering pump is not important to the driver.

Page 6: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Introduction to Inheritance• Classes can be organized using inheritance.

Page 7: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Introduction to Inheritance, cont.

• A class at lower levels inherits all the characteristics of classes above it in the hierarchy.

• At each level, classifications become more specialized by adding other characteristics.

• Higher classes are more inclusive; lower classes are less inclusive.

Page 8: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Introduction to Polymorphism• from the Greek meaning “many forms”• The same program instruction adapts to

mean different things in different contexts.– A method name, used as an instruction, produces

results that depend on the class of the object that used the method.

– everyday analogy: “take time to recreate” causes different people to do different activities

• more about polymorphism in Chapter 7

Page 9: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Outline

• Class and Method Definitions• Information Hiding and Encapsulation• Objects and Reference

Page 10: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Class and Method Definitions: Outline

• Class Files and Separate Compilation• Instance Variables• Using Methods• void Method Definitions• Methods that Return a Value• Accessing Instance Variables• Local Variables• Blocks• Parameters of a Primitive Type• Class and Method Definition Syntax

Page 11: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Basic Terminology

• Objects can represent almost anything.• A class defines a kind of object.

– It specifies the kinds of data an object of the class can have.

– It provides methods specifying the actions an object of the class can take.

• An object satisfying the class definition instantiates the class and is an instance of the class.

Page 12: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Basic Terminology, cont.

• The data items and the methods are referred to as members of the class.

• We will call the data items associated with an object the instance variables of that object (i.e. that instance of the class).

Page 13: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

A Class as an Outline

Page 14: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Class and Method Definition Syntax

public class Class_Name

{

Instance_Variable_Declaration_1;

Instance_Variable_Declaration_2;

Method_Definition_1

Method_Definition_2

...

}

Page 15: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Class and Method Definition Syntax, cont.

public Type_Name_Or_void Method_Name(Parameter_List);

where Parameter_List consists of– a list of one or more formal parameter

names, each preceded by a type and separated by commas or

– no formal parameters at all

Page 16: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Example

– The modifier public associated with the instance variables should be replaced with the modifier private, as we will do later in the chapter.

Page 17: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Class Files and Separate Compilation

• Each Java class definition should be in a file by itself.– The name of the file should be the same as

the name of the class.– The file name should end in .java

• A Java class can be compiled before it is used in a program– The compiled byte code is stored in a file

with the same name, but ending in .class

Page 18: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Class Files and Separate Compilation, cont.

• If all the classes used in a program are in the same directory as the program file, you do not need to import them.

Page 19: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Using Methods

• two kinds of methods:– methods that return a single value (e.g. nextInt)

– methods that perform some action other than returning a single value (e.g println), called void methods

Page 20: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Methods That Return a Value

• exampleint next = keyboard.nextInt();

– keyboard is the calling object.– You can use the method invocation any

place that it is valid to use of value of the type returned by the method.

Page 21: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Methods That Do Not Return a Value

• exampleSystem.out.println(“Enter data:”);

– System.out is the calling object.• The method invocation is a Java statement

that produces the action(s) specified in the method definition.– It is as if the method invocation were

replaced by the statements and declarations in the method definition.

Page 22: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

void Method Definitions

• examplepublic void writeOuput()

{

System.out.println(“Name: “ + name);

System.out.println(“Age: “ + age);

}

• Such methods are called void methods.

Page 23: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Method Definitions

• All method definitions belong to some class.• All method definitions are given inside the

definition of the class to which they belong.• If the definition of the method begins with

public void, it does not return a value.– public indicates that use is unrestricted.– void indicates that the method does not

return a value.

Page 24: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Method Definitions, cont.

• The parentheses following the method name contain any information the method needs.

• The first part of the method definition is called the heading.

• The remainder of the method is called the body, and is enclosed in braces {}.

• Statements or declarations are placed in the body.

Page 25: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

The Method main

• A program is a class that has a method named main.

• The programs we have seen so far have no instance variables and no methods other than method main.

• Programs can have instance variables and other methods.

Page 26: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Defining Methods That Return a Value

• examplepublic int fiveFactorial();

{

int factorial = 5*4*3*2*1;

return factorial;

}

• As before, the method definition consists of the method heading and the method body.– The return type replaces void.

Page 27: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Defining Methods That Return a Value, cont.

• The body of the method definition must containreturn Expression;

– This is called a return statement.– The Expression must produce a value of the

type specified in the heading.• The body can contain multiple return

statements, but a single return statement makes for better code.

Page 28: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Using return in a void Method

• formreturn;

• use– to end the invocation of the method,

usually prematurely, to deal with some problem

• caution– Almost always, there are better ways to

deal with a potential problem.

Page 29: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

public Method Definitions• syntax for a void method

public void Method_Name(Parameters);

{

<statement(s)>}

• syntax for methods that return a valuepublic Return_Type Method_Name(Parameters);

{

<statement(s), including a

return statement>}

Page 30: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Accessing Instance Variables

• Outside the class definition, a public instance variable is accessed with– the name of an object of the class– a dot (.)– the name of the instance variable.– Example: myBestFriend.name = “Lara”;

• Inside the definition of the same class only the name of the instance variable is used.– Example: name = keyboard.nextLine();

Page 31: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Accessing Instance Variables, cont.

• equivalent assignment statements:name = keyboard.nextLine();

andthis.name = keyboard.nextLine();

– The keyword this stands for the calling object - the object that invokes the method.

Page 32: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Local Variables

• A variable declared within a method is called a local variable.– Its meaning is “local to” (confined to) the

method definition.• Variables with the same name declared within

different methods are different variables.• A local variable exists only as long as the

method is active.

Page 33: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Local Variables, cont.• class BankAccount• class LocalVariablesDemoProgram

Page 34: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Blocks

• The terms block and compound statement both refer to a set of Java statements enclosed in braces {}.

• A variable declared within a block is local to the block.– When the block ends, the variable

disappears.• If you intend to use the variable both inside

and outside the block, declare it outside the block.

Page 35: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Variables in for Statements

• The loop control variable can be declared outside the for statementint n;

for (n = 1; n <10, n++)

in which case the variable n still exists when the for statement ends

or

Page 36: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Variables in for Statements, cont.

• The loop control variable can be declared inside the for statementfor (int n = 1; n <10, n++)

in which case the variable n ceases to exist when the for statement ends

Page 37: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Parameters of a Primitive Type

• Often it is convenient to pass one or more values into a method and to have the method perform its actions using those values.

• The values are passed in as arguments (or actual parameters) associated with the method invocation.

Page 38: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Parameters of a Primitive Type, cont.

• The method receives the values and stores them in its formal parameters (or simply parameters). – A method invocation assigns the values of

the arguments (actual parameters) to the corresponding formal parameters (parameters).

– This is known as the call-by-value mechanism.

• The formal parameters exist as long as the method is active.

Page 39: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Parameters of a Primitive Type, cont.

• Generally, the type of each argument must be the same as the type of the corresponding formal parameter.

• Java will perform automatic type conversion for an argument that appears to the left of a formal parameter it needs to matchbyte --> short --> int --> long --> float -->

double

Page 40: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Parameters of a Primitive Type, cont.

• An argument in a method invocation can be– a literal such as 2 or ‘A’– a variable– any expression that yields a value of the

appropriate type.

• A method invocation can include any number of arguments; the method definition contains a corresponding number of formal parameters, each preceded by its type.

Page 41: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Parameters of a Primitive Type, cont.

anObject.doStuff(42, 100, 9.99, ‘Z’);

public void doStuff(int n1, int n2, double d1, char c1);

– arguments and formal parameters are matched by position

• Everything said about arguments and formal parameters applies to methods that return a value as well as to void methods.

Page 42: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Method with a Parameter

Page 43: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Information Hiding and Encapsulation: Outline

• Information Hiding• Precondition and Postcondition Comments• The public and private Modifiers• Encapsulation• Automatic Documentation with javadoc• UML Class Diagrams

Page 44: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Information Hiding

• “Information overload” is avoided by suppressing or hiding certain kinds of information, making the programmer’s job simpler and the code easier to understand.

• A programmer can use a method defined by someone else without knowing the details of how it works (e.g. the println method)– He needs to know what the method does,

but not how it does it.

Page 45: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Information Hiding, cont.

• What the method contains is not secret, and maybe not even interesting.

• Viewing the code does not help you use the method and may distract you from the task at hand.

• Designing a method so that it can be used how it performs its task is called information hiding or abstraction.

Page 46: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

The public and private Modifiers

• The instance variables of a class should not be declared public.– Typically, instance variables are declared private.

• An instance variable declared public can be accessed and changed directly, with potentially serious integrity consequences.– Declaring an instance variable private

protects its integrity.

Page 47: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

The public and private Modifiers, cont.

• Analogy: An ATM permits deposits and withdrawals, both of which affect the account balance, but it does not permit an account balance to be accessed and changed directly. If an account balance could be accessed and changed directly, a bank would be at the mercy of ignorant and unscrupulous users.

Page 48: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

The private Modifier

• The private modifier makes an instance variable inaccessible outside the class definition.

• But within the class definition, the instance variable remains accessible and changeable.

• This means that the instance variable can be accessed and changed only via the methods accompanying the class.

Page 49: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Accessor and Mutator Methods

• Appropriate access to an instance variable declared private is provided by an accessor method which is declared public. – Typically, accessor methods begin with the

word get, as in getName.• Mutator methods should be written to guard

against inappropriate changes.

Page 50: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Encapsulation

• Encapsulation is the process of hiding details of a class definition that are not needed to use objects of the class.

• Encapsulation is a form of information hiding.

Page 51: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Encapsulation, cont.

• When done correctly, encapsulation neatly divides a class definition into two parts:– the user interface which communicates

everything needed to use the class– the implementation consisting of all the

members of the class.• A class defined this way is said to be well

encapsulated.

Page 52: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

The User Interface

• The user interface consists of– the headings for the public methods– the defined public constants– comments telling the programmer how to

use the public methods and the defined public constants.

• The user interface contains everything needed to use the class.

Page 53: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

The Implementation• The implementation consists of

– the private instance variables– the private defined constants– the definitions of public and private

methods.• The Java code contains both the user

interface and the implementation.• Imagine a wall between the user interface

and the implementation.

Page 54: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Encapsulation

Page 55: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Encapsulation Guidelines

• Precede the class definition with a comment that shapes the programmer’s view of the class.

• Declare all the instance variables in the class private.

• Provide appropriate public accessor and mutator methods.

Page 56: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Encapsulation Guidelines, cont.

• Provide public methods to permit the programmer to use the class appropriately.

• Precede each public method with a comment specifying how to use the method.

• Declare methods invoked only by other methods in the class private.

• Use /*...*/ or /**...*/ for user interface comments and // for implementation comments.

Page 57: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Encapsulation Characteristics

• Encapsulation should permit implementation changes (improvements, modifications, simplifications, etc.) without requiring changes in any program or class that uses the class.

• Encapsulation combines the data and the methods into a single entity, “hiding” the details of the implementation.

Page 58: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Objects and Reference: Outline

• Variables of a Class Type and Objects• Boolean-Valued Methods• Class Parameters• Comparing Class Parameters and Primitive-

Type Parameters

Page 59: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Variables• Variables of a class type name objects, which

is different from how primitive variables store values.

• All variables are implemented as memory locations.– The value of a variable of a primitive type

is stored in the assigned location.– The value of a variable of a class type is

the address where a named object of that class is stored.

Page 60: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Variables, cont.

• A value of any particular primitive type always requires the same amount of storage.– example: a variable of type int always

requires 4 bytes.• An object of a class type might be arbitrarily

large.– An object of type String might be empty, or

might contain 1, 120, 5280, or more characters.

Page 61: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Variables, cont.

• However, there is always a limit on the size of an address.

• The memory address where an object is stored is called a reference to the object.

• Variables of a class type behave differently from variables of a primitive type.

Page 62: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Variables of a Primitive Type

• Only one variable of a primitive type is associated with the memory location where the value of the variable is stored.

Page 63: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Variables of a Class Type

• However, multiple variables of a class type can contain the memory location where an object is stored.– The object can be accessed and potentially

changed using any of these variables.– All these variables contain the same

reference and name the same object.• Variables of a class type referencing the

same memory location are called aliases.

Page 64: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Variables of a Class Type, cont.

• When one variable of a class type is assigned to another variable of a compatible class type, the memory address is copied from one variable to another and the two variables become aliases of one another.

• The numeric values of these memory addresses generally are not available to the programmer nor are they needed by the programmer.

Page 65: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Allocating Memory for a Reference and an Object

• A declaration such asSpeciesFourthTry s;

creates a variable s that can hold a memory address.

• A statement such ass = new SpeciesFourthTry();

allocates memory for an object of type SpeciesFourthTry.

Page 66: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

== with Variables of a Class Type

Page 67: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

== with Variables of a Class Type, cont.

• When used with variables of a class type, == tests if the variables are aliases of each other, not if they reference objects with identical data.

• To test for equality of objects in the intuitive sense, define and use an appropriate equals method.

Page 68: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Method equals• The definition of method equals depends on

the circumstances.– In some cases, two objects may be “equal”

when the values of only one particular instance variable match.

– In other cases, two objects may be “equal” only when the values of all instance variables match.

• Always name the method equals.

Page 69: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Class Parameters

• Recall– When the assignment operator is used with

objects of a class type, a memory address is copied, creating an alias.

– When the assignment operator is used with a primitive type, a copy of the primitive type is created.

Page 70: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Class Parameters, cont.

– When a parameter in a method invocation is a primitive type, the corresponding formal parameter is a copy of the primitive type.

Page 71: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Class Parameters, cont.

• When a parameter in a method invocation is a reference to a class type (i.e. a named object), the corresponding formal parameter is a copy of that reference (i.e. an identically valued reference to the same memory location).

Page 72: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Class Parameters, cont.

• Exampleif (s1.equals(s2))

public boolean equals (Species otherObject)

causes otherObject to become an alias of s2, referring to the same memory location, which is equivalent tootherObject = s2;

Page 73: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Class Parameters, cont.

• Any changes made to the object named otherObject will be done to the object named s2, and vice versa, because they are the same object.– If otherObject is a formal parameter of a

method, the otherObject name exists only as long as the method is active.

Page 74: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Comparing Class Parameters and Primitive-Type

Parameters

• A method cannot change the value of a variable of a primitive type passed into the method.

• A method can change the value(s) of the instance variable(s) of a class type passed into the method.

Page 75: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Comparing Class Parameters and Primitive-Type Parameters, cont.

Page 76: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Comparing Class Parameters and Primitive-Type Parameters, cont.

Page 77: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Summary

• You have become familiar with the concept of a class and an object that instantiates the class.

• You have learned how to define classes in Java.

• You have learned how to define and use methods in Java.

• You have learned how to create objects in Java

Page 78: Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,

Summary, cont.

• You have learned how parameters work in Java.

• You have learned about information hiding and encapsulation.

• You have become familiar with the notion of a reference (to understand class variables and class parameters).