Java Reference
In-Depth Information
4
Defining Classes I
This is the exciting part.
This is like the Supremes.
See the way it builds up?
Attributed to FRANK ZAPPA
Introduction
Classes are the single most important language feature that facilitates object-oriented
programming (OOP), the dominant programming methodology in use today. You
have already been using predefined classes.
are two of the classes
we have used. An object is a value of a class type and is referred to as an
and
String
Scanner
instance of the
An object differs from a value of a primitive type in that it has methods (actions)
as well as data. For example,
class.
. It has the charac-
ters in the string as its data and also has a number of methods, such as
is an object of the class
"Hello"
String
. You
already know how to use classes, objects, and methods. This chapter tells you how to
define classes and their methods.
length
Prerequisites
This chapter uses material from Chapters 1, 2, and 3.
4.1
Class Definitions
“The Time has come,” the Walrus said,
“to talk of many things:
of shoes and ships and sealing wax
of cabbages and kings.”
LEWIS CARROLL,
Through the Looking-Glass
A Java program consists of objects from various classes interacting with one another.
Before we go into the details of how you define classes, let's review some of the termi-
nology used with classes. Among other things, a class is a type and you can declare
variables of a class type. A value of a class type is called an
object
. An object has both
object
method
data and actions. The actions are called
. Each object can have different data,
but all objects of a class have the same types of data and all objects in a class have the
same methods. An object is usually referred to as an object of the class or as an
methods
Search WWH ::




Custom Search