Java Reference
In-Depth Information
For example, a Cow object in Minecraft has a lot of functions you can call.
Here are a few of them:
teleportTo(Location location )
setFireTicks(int ticks )
It also maintains some internal state: some variables to keep track of that
individual cow's location, whether it's on fire, its age, and so on.
These are all fine things you can do with a Cow object. But they wouldn't all
make sense for a player, or an arrow, or a tree.
You want to keep cow things in the Cow object, arrow things in the Arrow object,
and so on. If you don't, then things can get awfully confusing awfully fast,
and you might end up writing a pile of code that you can no longer understand
or work with. So this is really just a matter of good hygiene, like keeping milk
in the milk carton in the fridge and not storing it in the pretzel bag in the
closet. You don't even want to mix corn chips with potato chips, to continue
with this metaphor.
Keeping separate things separate in different objects is the easy part.
The hardest aspect of programming is this problem of having to deal with
very low-level details and very high-level details at the same time. We call
these levels of abstraction. Let's face it: when you're writing code to deal with
a player in Minecraft, it's not the actual person playing the game. Somewhere
there is an actual person, sitting there, sweating, eating chips and listening
to loud music. Your piece of code is an abstract representation of that real-
life player; an abstraction that includes the data and behavior you need for
the game.
And just as in the real world, each abstraction can contain parts. So you can
choose to focus on molecules or planets as you need to—or in software, on
the molecule objects or planet objects. You can zoom in and out to the level
you need to be at, and work with the parts you want.
Let's take a closer look at what that means in Minecraft and Java.
Combine Data and Instructions into Objects
Suppose you were to write a game like Minecraft from scratch. You'd want to
have a bunch of players in the game. Each one would have its own name,
Search WWH ::

Custom Search