Java Reference
In-Depth Information
CHAPTER
1
Objects and classes
Main concepts discussed in this chapter:
objects
methods
classes
parameters
It's time to jump in and get started with our discussion of object-oriented programming.
Learning to program requires a mix of some theory and a lot of practice. In this topic, we will
present both, so that the two reinforce each other.
At the heart of object orientation are two concepts that we have to understand first: objects and
classes. These form the basis of all programming in object-oriented languages. So let us start
with a brief discussion of these two foundations.
1.1
Objects and classes
If you write a computer program in an object-oriented language, you are creating, in your com-
puter, a model of some part of the world. The parts that the model is built up from are the objects
that appear in the problem domain. These objects must be represented in the computer model be-
ing created. The objects from the problem domain vary with the program you are writing. They
may be words and paragraphs if you are programming a word processor, users and messages if
you are working on a social-network system, or monsters if you are writing a computer game.
Concept:
Java objects
model objects from
a problem domain.
Objects may be categorized as—and a class describes, in an abstract way—all objects of a par-
ticular kind.
We can make these abstract notions clearer by looking at an example. Assume you want to
model a traffic simulation. One kind of entity you then have to deal with is cars. What is a car in
our context: Is it a class or an object? A few questions may help us to make a decision.
Concept:
Objects are created
from classes . The
class describes the
kind of object; the
objects represent
individual instantia-
tions of the class.
What color is a car? How fast can it go? Where is it right now?
You will notice that we cannot answer these questions until we talk about one specific car. The
reason is that the word “car” in this context refers to the class car; we are talking about cars in
general, not about one particular car.
 
 
 
Search WWH ::




Custom Search