Java Reference
In-Depth Information
Why Bother Using Objects?
Imagine you are a god of your own universe. You have spent a few eons
arranging every quark, every atom, every molecule, all the way up to planets
and galaxies, just the way you want it. Now the whole thing spins up, and
you're responsible for every single subatomic particle in the whole universe,
all at once. Even as a godlike being, trying to keep the universe going by
dealing with every electron or every quark or even every molecule is just too
much work (not to mention incredibly boring).
So instead, you deal with problems on the scale in which they occur. If it's a
problem with a planet in the wrong spot, you move the planet. If you need to
fiddle with a galaxy, you fiddle with the galaxy—not with every planet, and
certainly not with every life form on every planet in every system.
Although it might sound grandiose, creating a program is very much like that.
You're a very powerful creator of your own little universe. Maybe not exactly
godlike, but you do have to face that same issue of dealing with things at a
very low level, like atoms or molecules, and at a very high level, like creatures,
mountains, planets, and galaxies. All at once. When programming, you often
have to zoom in to “atoms” and zoom out to “galaxies.” They're all connected
and have to make sense.
That's why we write code using objects. It's a way to organize data (in variables)
and behavior (in functions) so that when we want to deal with a cow, we can
treat it like a cow and not have to deal with each of the millions of atoms that
make up said cow—or a biome, or a world, or a torch.
Even better, we can write code so that only a Cow has functions and data a
cow needs. There's nothing worse than having assorted functions spilling out
all over the place—you might end up with a torch that moos or a cow that
lights up. Worse still, you'll end up with a huge pile of functions where you're
not really sure which function can work with which piles of data.
 
 
Search WWH ::




Custom Search