Previous | Next | Trail Map | Writing Java Programs | Object-Oriented Programming Concepts: A Primer


What is an Object?

As the name implies, objects are the key concept to understanding object-oriented technology. You can look around you now and see many examples of real-world objects: your dog, your desk, your television set, your bicycle.

These real-world objects share two characteristics: they all have state and they all have behaviour. For example, dogs have state (name, color, breed, hungry) and dogs have behaviour (barking, fetching and slobbering on your newly cleaned slacks). Bicycles have state (current gear, current pedal cadence, two wheels, number of gears) and behaviour (braking, accelerating, slowing down, changing gears).

Software objects are modelled after real-world objects in that, they too, have state and behaviour. A software object maintains its state in variables and implements its behaviour with methods.


Definition: Objects are software bundles of data and related methods.
Because software objects are modeled after to real-world objects, you can represent real-world objects in programs using software objects. You might want to represent dogs in an animation program or a bicycle in an indoor electronic exercise bike. However, you can also use software objects to "objectify" abstract concepts. For example, "event" is a common object used in GUI window systems to represent the event when a user presses a mouse button or strikes a key on the keyboard.

The following illustration is a common visual representation of a software object:

Everything that the software object knows (state) and can do (behaviour) is expressed by the variables and methods within that object. A software object that modelled your real-world bicycle would have variables that indicated the bicycle's current state: it's moving at 10 mph, its pedal cadence is 90 rpm, and it's in 5th gear.

The software bicycle would also have methods that allowed you to brake, change the pedal cadence and change gears. (The bike would not have a method for changing the speed of the bicycle as the bike's speed is really just a side-effect of what gear you're in, how fast you're pedaling and how steep the hill is.)

Anything that an object does not know or can not do is excluded from the object. For example, your bicycle (probably) doesn't have a name, and it can't run, bark or fetch. Thus there are no variables or methods for those states and behaviours.

As you can see from the diagrams, the object's variables make up the center or nucleus of the object and the methods surround and hide the object's nucleus from other objects in the program. Packaging an object's variables within the protective custody of its methods is called encapsulation. Typically, encapsulation is used to hide unimportant implementation details from other objects. When you want to change gears on your bicycle, you don't need to know how the gear mechanism works, you just need to know which lever to move. Thus, the implementation can change at any time without changing other parts of the program.

The Benefit of Encapsulation

Encapsulating related variables and methods into a neat software bundle is a simple yet very powerful idea that provides two primary benefits to software developers:


Previous | Next | Trail Map | Writing Java Programs | Table of Contents