Monday, April 12, 2010

CLASSESAND OBJECT

Now that we have introduced operators, expressions, statements, and methods, we can finally talk about classes. A class is
a named collection of fields that hold data values and methods that operate on those values. Some classes also contain
nested inner classes. Classes are the most fundamental structural element of all Java programs. You cannot write Java code
without defining a class. All Java statements appear within methods, and all methods are defined within classes.
Classes are more than just another structural level of Java syntax. Just as a cell is the smallest unit of life that can survive
and reproduce on its own, a class is the smallest unit of Java code that can stand alone. The Java compiler and interpreter do
not recognize fragments of Java code that are smaller than a class. A class is the basic unit of execution for Java, which
makes classes very important. Java actually defines another construct, called an interface, that is quite similar to a class.
The distinction between classes and interfaces will become clear in Chapter 3, "Object-Oriented Programming in Java", but
for now I'll use the term "class" to mean either a class or an interface.
Classes are important for another reason: every class defines a new data type. For example, you can define a class named
Point to represent a data point in the two-dimensional Cartesian coordinate system. This class can define fields (each of
type double) to hold the X and Y coordinates of a point and methods to manipulate and operate on the point. The Point
class is a new data type.
When discussing data types, it is important to distinguish between the data type itself and the values the data type
represents. char is a data type: it represents Unicode characters. But a char value represents a single specific character. A
class is a data type; the value of a class type is called an object. We use the name class because each class defines a type (or
kind, or species, or class) of objects. The Point class is a data type that represents X,Y points, while a Point object
represents a single specific X,Y point. As you might imagine, classes and their objects are closely linked. In the sections
that follow, we will be discussing both.

No comments: