Many objects differ from each other only in the value of the data that they hold. For example, both a red crayon and a blue crayon are crayons; they differ only in the value of the color attribute, one has a red color and the other a blue color. Our object-oriented system needs a way to capture the abstraction of a crayon, independent of the value of its color. That is, we want to express that a set of objects are abstractly equivalent, differing only in the values of their attributes and perhaps, thus differing in the behaviors that result from those values.
Many objects are similar in many overall, generalized ways, differing only in smaller, more specify details. In biology and other fields, scientists organize objects into taxonomies, which are classification hierarchies used to express these similarities. For instance, a butterfly and a lobster are quite different, yet they share the common characteristics of all Arthropods, such as a jointed exoskeleton. The notion of an Arthropod enables us to understand and deal with butterflies and lobsters in an abstract, unified manner. So once again we are trying to express abstract equivalence.
Object-oriented systems use classes to express the above notions of abstract equivalence.
A class is an abstract description of a set of objects.
A class thus contain the descriptions of all the behaviors of the objects that it represents. In computer science parlance, we call the individual behaviors of a class its methods. In addition, a class may, but not always, contain descriptions of the internal data held by the objects, called its fields, as well as implementation details about its methods and fields.
Turning the description around, we can say that a class is a template or recipe for the creation of a particular type of object. That is, one can use a class to create ("instantiate") objects of the type described by the class. Be careful not to make the very beginner's common mistake of equating classes and objects. A class is a specification of an set of objects, it is not the actual object.
In technical terms, a class defines a new type in the system. Types are identifies used to differentiate different kinds of data. For instance, integers, characters, strings and arrays are all types of data.
- 1349 reads