Held Friday, September 3, 1999
Today, we will consider how one writes and uses classes and objects
in Java. Most of this consideration will be through a laboratory.
- Don't forget that you're supposed to read the discussion section of
each lab before coming to class.
- We've decided to write an IMAP/MES mail client
as the class project.
- Yes, we have class on Monday (even though it's labor day).
- Lab J3 (for Monday)
- Think about the parts of a mail client.
- As we've seen, object-oriented programming involves not just objects,
but also classes that provide generic information about
groups of similar objects.
- The kinds of attributes these objects will have (e.g., every book
has a title). These are usually called fields.
- The facilities these objects will have. These are usually called
- In Java, most of your programming will center around classes.
- As you've seen, each class will go in its own file.
- What goes in a class?
- A class definition begins with the keywords
class class and is followed
by the name of the class and the body of the class, surrounded in
- Later, you'll see some variations.
- What does in the body of the class?
- The methods.
- The fields.
- Not much else.
- We've talked about much of this already, but it doesn't hurt
- Methods provide functionality for classes and objects.
- They are like the functions you know from Scheme.
- Unlike Scheme functions, Java methods must also be typed.
This means that you must specify what kind of information each method
takes as parameters, and what kind of information each method returns.
- A print method might take something to print as a parameter and
- An average method might take numeric values to average as parameters
and return a number.
- Hence each method has
- a name (so that we can tell which method we're using),
- a return type (what kind of value it returns),
- a parameter list (giving names and types of parameters),
- a body (describing what it does), and
- a number of optional components that we won't yet
concern ourselves with.
- There are two kinds of methods:
- object methods require an instantiated object to work;
- class methods do not require objects.
- Object methods can refer to all the attributes of the object, as well
as to other methods of the object.
- For example, a
Printer object might provide a
print method for printing to the associated printer.
- Class methods can only refer to class attributes and class methods
(they can, of course, refer to object methods in conjunction with an
object of the same class).
- For example, a
average method would not need to knowk
anything about a particular object (other than its parameters).
- We will almost exclusively use object methods.
Do sections J2.1, J2.2A, J2.3, J2.4, J2.5, and J2.7 of
If you cannot finish all the sections during class, please complete
them over the weekend.
- What were some of the interesting things you learned from this lab?
- What was the most challenging part of this lab?