Java Reference
In-Depth Information
A class that defines a print job (such as gets created when you click Print in a word
Notice that I always say “a class.” All objects in Java are classes. Enumerations are a special kind of
class, because each enumeration has an implicit class provided by the compiler. Java also has primitives
and interfaces, but primitives and interfaces are not objects. As we saw in earlier chapters, primitives
define values, but they aren't objects. We get to why neither primitives nor interfaces are objects shortly.
For now, just remember that a Java object is an instance of a class, and a class is code that defines
something useful. Even an abstract class defines something useful, though it relies on the classes that
extend it to provide the remaining information.
Object-oriented languages support three important features:
We look at these in turn.
Encapsulation means that an object holds its contents in such a way that other objects can't see or
change those contents (though we have a number of ways to provide access to the contents of a class).
That way, one object's contents can't get tangled up with the contents of another object. Given that an
application can have thousands of classes, keeping the bits and pieces of all those classes separate is a
really good idea. Otherwise, we'd never be able to find anything.
One of the key concerns of a Java developer when creating a new class (or modifying an existing
one) is figuring out which bits and pieces to make visible to other classes. Another, related, concern is
which classes get to see (and possibly modify) the contents of the current class. As that implies, you can
make part of a class visible to just selected other classes rather than all other classes. We cover the
mechanics of access modifiers in Chapter 3, “Data Types”. We cover when you should use the various
access modifiers in the next chapter.
Inheritance defines relationships between the classes (and so between the objects) within an object-
oriented system. All classes in Java have some other object as a parent. Java developers say that one
object extends another. If you think about it for a minute, you shortly realize that there must be some
object that starts it all. That special object (the only one that has no parent) is a class called Object . All
other Java objects are descendants (sometimes through many levels) of Object .
Let's consider a simple example from the field of Biology (with apologies to biologists for vastly
over-simplifying and thanks to Caroline Valentine at Valentine Human Resources for the idea). Suppose
we want to represent certain animals (in particular, cats, dogs, and mice). They're all mammals, so we
might start with a class called Mammal .
Then we have three more classes called Cat , Dog , and Mouse . Each of those classes extend the Mammal
Search WWH ::

Custom Search