Creating new
data types: class
If everything is an object, what determines how a particular class of object looks and behaves? Put another way, what establishes the type of an object? You might expect there to be a keyword called “type,” and that certainly would have made sense. Historically, however, most object-oriented languages have used the keyword class to mean “I’m about to tell you what a new type of object looks like.” The class keyword (which is so common that it will not be bold-faced throughout this book) is followed by the name of the new type. For example:
class ATypeName { /* Class body goes here */ }
This introduces a new type, although the class body consists only of a comment (the stars and slashes and what is inside, which will be discussed later in this chapter), so there is not too much that you can do with it. However, you can create an object of this type using new:
ATypeName a = new ATypeName();
But you cannot tell it to do much of anything (that is, you cannot send it any interesting messages) until you define some methods for it.