Title: Chapter 5 Part 2
1Chapter 5 Part 2
- COSI 11a
- 2006-11-08
- Prepared by Ross Shaull
2Objectives for Today
- Review encapsulation.
- Discuss the idea of a class interface, and how we
use the tools we have learned to create a useful
one. - Learn about packages.
- Write more code in GuessWord.java.
3Encapsulation
- We sometimes call it information hiding.
- Who can give me some intuitions or definitions
for encapsulation? - Can the tools weve learned in chapters 4 and 5
help us create a well encapsulated class? (yes!)
4Friendly Interface
- Put yourselves in the shoes of another programmer
who is going to use the class youve written. - You want to give this programmer an interface to
your class that is - Safe
- Easy to understand
- Easy to use
5Safe
- Constructors that either
- Provide parameters to set necessary values
corresponding to internal state (i.e. instance
variables) or, - Set reasonable defaults for internal state not
provided as argument to constructor. - Private variables and final variables
- How do these improve safety?
6Easy to Understand
- Overloaded methods reduce the number of method
names that programmers have to remember. - Proper return values and parameters make it clear
what a method does. - Good comments that can be extracted using javadoc.
7Easy to Use
- Reduce keystrokes!
- Static methods help because you dont have to
instantiate an object to use them.
8Why?
- Programs are most useful when they can be used
multiple times, perhaps in contexts you didnt
even think of. - For example, the GuessGame class that we are
writing might be useful as a component in
another, more complicated program (who is
familiar with Wheel of Fortune?).
9Why?
This makes your programs easier for you to
understand, too!
- This idea of reusing code is part of the core
philosophy behind Object Oriented design
(remember that term from the first lecture?). - When you keep these principles in mind, your
classes become more useful for other programmers.
10Packages
- Get organized!
- Keep code in multiple directories.
- Resolve name clashes.
- Works hand-in-hand with good OO design to make
your classes easier to use for other programmers
(and you). - Page 384.
11Package Declaration
package general.utilities public class
DoubleOut // stuff here
This file must be stored in a directory
general/utilities
general/utilities can be put in a variety of
library paths!
Remember the CLASSPATH variable?
12Package Example (Windows)
C\myjavastuff\libraries\general\utilities\DoubleO
ut.java
CLASSPATH C\myjavastuff\libraries.
import general.utilities.DoubleOut public class
DoubleOutTest // use DoubleOut in here
The . is very important!
13Package Example (OS X)
/Users/ross/java/lib/general/utilities/DoubleOut.j
ava
CLASSPATH /Users/ross/java/lib/.
import general.utilities.DoubleOut public class
DoubleOutTest // use DoubleOut in here
The . is here again!
14Summary (Ask Questions!)
- Invoking methods
- Method parameters
- Static methods
- Constructors
- Information Leakage
- Testing techniques
- Packages