Ch08 Objects and Classes
Ch08 Objects and Classes
Chapter Goals
Class
Private Data
(Variables)
Public Interface
(Methods)
Self Check 8.1
Is the method call "Hello, World".println() legal? Why or why not?
Answer: No––the object "Hello, World" belongs to the String class,
and the String class has no println method.
Self Check 8.2
When using a String object, you do not know how it stores its characters. How can
you access them?
Answer: Through the substring and charAt methods.
Self Check 8.3
Describe a way in which a String object might store its characters.
Each object instantiated from the class has its own set of instance variables
Each tally counter has its own current count
Access Specifiers:
Classes (and interface methods) are public
Instance variables are always private
Instantiating Objects
Objects are created based on classes
Use the new operator to construct objects
Give each object a unique name (like variables)
You have used the new operator before:
/**
A simulated cash register that tracks the item count
and the total amount due.
*/ Javadoc style comments
public class CashRegister document the class and the
{ behavior of each method
/**
Adds an item to this cash register.
@param price: the price of this item
*/
public void addItem(double price)
{
// Method body The method declarations make up
} the public interface of the class
/**
Gets the price of all items in the current sale.
@return the total price The data and method bodies make up
*/ the private implementation of the class
public double getTotal() ...
Non-static Methods Means…
We have been writing class methods using the static modifier:
For non-static (instance) methods, you must instantiate an object of the class
before you can invoke methods
But you can create a new object using your existing reference
An object is manipulated through the public interface (front of the card)
The encapsulated data is on the back of the card
Mutator Methods and Cards
As mutator methods are called, keep track of the value of instance variables
Object Reference
Shared References
CashRegister reg2 = reg1;
if (middleInitial == null)
System.out.println(firstName + " " + lastName);
else
System.out.println(firstName + " " + middleInitial + ". "
+ lastName);
The this Reference
Methods receive the ‘implicit parameter’ in a reference variable called this
It is a reference to the object the method was invoked on:
public BankAccount()
{
lastAssignedNumber++;
accountNumber = lastAssignedNumber;
}
. . .
}
The package name consists of one or more identifiers separated by periods (see
8.12.3)
package com.horstmann.bigjava;
public class Financial
{
. . .
}
Using package names in this way can be inconvenient, so Java allows classes to
be imported
import java.util.Scanner;
Imported classes can be referenced without using the package name prefix
Importing Multiple Classes
You can import all classes of a package with an import statement that ends in .*
This import statement allows you to refer
import java.util.*; to the Scanner or Random classes without
qualifying the names with java.util.
You never need to import the classes in the java.lang package explicitly
Effectively, an automatic import java.lang.*; statement has been placed into
every source file
You don’t need to import other classes in the same package
Syntax 8.4 Package Specification
Organize the classes in your source file with a package statement
Package Names
When two classes have the same name, name clashes are avoided by putting
them into different packages
Two different Timer classes
java.util.Timer
javax.swing.Timer
Unique package names are often constructed by reversing domain names or email
addresses
com.horstman
walters@cs.sjsu.edu
Package and Source Files
A source file must be located in a subdirectory that matches the package name
Parts of a package name between periods represent successively nested
directories