Custom Search

Java: Object

As a god of cyber-space you are going to create a cyber-universe - a Java program. In that universe you are going to create objects - they will be the only 'things' in your universe.

What is an object?

A real-world object can be defined in two ways:

  • - what it is - what characteristics it has at this moment- its 'state' and
  • - what it can do - what action could be done to change its state - a 'method' that could be applied to the object

For example: a lamp

It can have two states: 'on' and 'off'.

The method to change that state would be 'switching'.

A lamp is a simple object - but some objects are very complex with lots of states and a wealth of methods that could be applied to them to change those states.

Some objects also contain other objects. In reality the lamp is more complex than the system for it we have described - it contains a bulb and a switch. The state of the switch affacts the state of the bulb.

A software object.

A software object stores its state in fields (also called variables in some programming languages) and changes those states via methods (called functions in some programming languages).

Methods

Methods operate on an object's internal state. They change the state of an object. In Java we say be 'invoke' a method. The method is like a spell we cast on the object - changing its state... or to link to our cyber-god analogy we 'make it so' via a method.

Methods also serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation — a fundamental principle of object-oriented programming.

Consider a bicycle modeled as a software object, for example:

By attributing state (current speed, current pedal cadence, and current gear) and providing methods for changing that state we can make the cyber-bicycle behave in a similar way to one in the real world.

When we write the method for changing gear we have to make it only possible to change gear within the number of gears we have selected. For example, if the bicycle only has 6 gears, a method to change gears should be written in a way that rejects any value that is less than 1 or greater than 6.

Why use object orientated programming?

Bundling code into individual software objects provides a number of benefits, including:

  • Modularity: The source code for an object can be written and maintained independently of the source code for other objects. Once created, an object can be easily passed around inside the system.
  • Information-hiding: By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world. Keep the 'magic' secret... ;)
  • Code re-use: If an object already exists (perhaps written by another software developer with far more experience that you), you can use that object in your program. You can build up a bank of objects that you trust and have tested and assemble them into a new program. Collecting useful code snippets is a valuable past time!
  • Pluggability and debugging ease: If a particular object turns out to be a problem, you can simply remove it from your application and plug in a different object as its replacement. In the real world if a fuse blows in a lamp, you replace the fuse, you don't get a new lamp.