Java Reference
In-Depth Information
layout of the rooms and other crucial characteristics are the same in each. To cre-
ate a different house, we would need a different blueprint.
A class is a blueprint of an object. It establishes the kind of data an object of
that type will hold and defines the methods that represent the behavior of such
objects. However, a class is not an object any more than a blueprint is a house. In
general, a class contains no space to store data. Each object has space for its own
data, which is why each object can have its own state.
Once a class has been defined, multiple objects can be created
from that class. For example, once we define a class to represent the
concept of a bank account, we can create multiple objects that rep-
resent specific, individual bank accounts. Each bank account object
would keep track of its own balance.
An object should be encapsulated, which means it protects and manages its
own information. That is, an object should be self-governing. The only changes
made to the state of the object should be accomplished by that object's methods.
We should design objects so that other objects cannot “reach in” and change their
states.
Classes can be created from other classes by using inheritance. That is, the defi-
nition of one class can be based on another class that already exists. Inheritance is
a form of software reuse, capitalizing on the similarities between various kinds of
classes that we may want to create. One class can be used to derive several new
classes. Derived classes can then be used to derive even more classes. This creates
a hierarchy of classes, where the attributes and methods defined in one class are
inherited by its children, which in turn pass them on to their children, and so on.
For example, we might create a hierarchy of classes that represent various types
of accounts. Common characteristics are defined in high-level classes, and specific
differences are defined in derived classes.
Polymorphism is the idea that we can refer to multiple types of related objects
over time in consistent ways. It gives us the ability to design powerful and elegant
solutions to problems that deal with multiple objects.
Some of the core object-oriented concepts are depicted in Figure 1.23. We
don't expect you to understand these ideas fully at this point. Most of this topic
is designed to flesh out these ideas. This overview is intended only to set the stage.
KEY CONCEPT
A class is a blueprint of an object.
Multiple objects can be created from
one class definition.
 
Search WWH ::




Custom Search