Java Reference
In-Depth Information
Chapter 2. Classes and Objects
First things first, but not necessarily in that order.
Dr. Who, Meglos
The fundamental programming unit of the Java programming language
is the class. Classes provide the structure for objects and the mechan-
isms to manufacture objects from a class definition. Classes define meth-
ods: collections of executable code that are the focus of computation and
that manipulate the data stored in objects. Methods provide the behavior
of the objects of a class. Although you can compute using only primit-
ive typesinteger, floating-point, and so onalmost any interesting program
will create and manipulate objects.
Object-oriented programming strictly separates the notion of what is to
be done from how it is done. "What" is described as a set of methods
(and sometimes publicly available data) and their associated semantics.
This combinationmethods, data, and semanticsis often described as a
contract between the designer of the class and the programmer who uses
it because it says what happens when certain methods are invoked on
an object. This contract defines a type such that all objects that are in-
stances of that type are known to honor that contract.
A common assumption is that the methods declared in a class are its en-
tire contract. The semantics of those operations are also part of the con-
tract, even though they may be described only in documentation. Two
methods may have the same name and parameters and throw the same
exceptions, but they are not equivalent if they have different semantics.
For example, not every method called print can be assumed to print a
copy of the object. Someone might define a print method with the se-
mantics " pr ocess int erval" or " pri oritize n on t erminals"not that we'd re-
commend this. The contract of the method, both signature and semantics
together, defines what it means.
The "how" of an object is defined by its class, which defines the imple-
mentation of the methods the object supports. Each object is an instance
 
Search WWH ::




Custom Search