Held: Friday, 12 March 2004
Today we consider how to make classes and objects that encapsulate values
and provide capabilities.
- Building Classes.
- The Problem: A RationalClass.
- Overview of a Class Declaration.
- Fields, Methods, and Constructors.
- Different Class Uses.
- Some Design Issues.
- Now that we've written some simple programs, we're ready to
consider more details about the construction of classes.
- Remember that a class provides a template for the construction of
- There are three key parts to each class:
- The fields that categorize the attributes of objects
in the class.
- The constructors that allow us to construct new objects
in the class.
- The methods that describe and implement the capabilities
of objects in the class.
- We'll consider each in turn as we attempt to build a simple
- We begin by identifying potential attributes, methods, and
- (Intentionally left out of the outline.)
- You must enclose the fields, methods, and constructors of a
class in a class declaration.
- Each class gets its own file (at least in CS152).
- The typical structure of a class is as follows.
* A helpful introductory comment that describes the class.
public class ClassName
// Field declarations
} // class ClassName
- There are three parts to a field declaration. In reverse order,
- The name of the field.
- The type of the field. (The kinds of values it can take on.)
- The word
- For example,
- You may have noted that field declarations look a lot like
variable declarations. How do they differ?
- Field declarations are part of a class; variable declarations
are part of a method definition.
- Fields are available
anywhere in the class; variables
are available only in the method they are declared in.
- Fields have protection levels; variables do not.
- Variables may be initialized when they are declared;
fields typically are not.
- When you reference fields, you should preface them with
- What should the fields for our class look like?
- The things you called functions in Scheme we call
methods in Java.
- There are typically 6 parts to a method declaration.
- A comment that describes the purpose of the method.
- The protection level of the method (typically
public). + The type of value the method returns. You can use
void when a method doesn't return anything.
- The name of the method.
- The parameters of the method.
- The body of the method.
- For example
public String getName()
} // getName()
public void setName(String newName)
this.name = newName;
} // setName(String)
- We tend to classify methods as observers and modifiers.
- Observers extract a value from the object without
changing the object.
- Modifiers change the object.
- Constructors are special methods used to create new objects
within the class. They are much like methods except that
you don't include the type.
- Whenever you build a new object in a class, you call a
- Although I haven't mentioned it explicitly, Java programmers
use classes for a number of reasons. I typically classify them
as template classes, main classes, and
- Template classes provide templates for creating new objects. Our
Fraction classes are template
- Template classes are the most common classes.
- Main classes provide a
main method. You can
think of the
main method as a combination of the script
and directory of a play. This method creates the objects that form
actors of the play that is your program. It then
tells them what to do.
- Utility classes (which you haven't really encountered yet)
provide methods for manipulating other objects. These methods are
primarily class/static methods.
- Some classes are a hybrid of these kinds of classes.
- In object-oriented design (and in all program design) there are
often many different ways to solve the same problem.
- For example, there are at least three ways to print a point:
- You can ask the point for information about itself and then
use a SimpleOutput object to print that information.
- You can hand the point a SimpleOutput object and ask the point
to print itself.
- You can hand both the point and the SimpleOutput object to
a helper and ask the helper to print out the point.
- We'll spend a little while brainstorming about the advantages and
disadvantages of each. We may ground our discussion in typical
- What if you want to print many different kinds of values?
- What if you want to print points in different ways?