Skip to content
Chicago Tribune
PUBLISHED: | UPDATED:
Getting your Trinity Audio player ready...

So far in this column, we’ve kicked around a lot of terms related to object-oriented programming, including object-oriented analysis, object-oriented design and object methodology.

By now you might be wondering, “What exactly is an object?” or, “What does an object look like and how can I create one?”

In this installment of Hooked on Objects, we’ll address these questions as we get our virtual hands dirty and explore the anatomy of an object.

The object

The term “object” means different things to different people because there are more ways than one to define it. Just as there are different programming languages for coding such as Java or C++, there are different object methodologies for defining OO lingo.

Object methodologies define a common language, semantics and rules for defining and describing objects. It is not our goal to try to convince you that one methodology is best; we’ll leave that to personal taste or corporate edict. For consistency and the purposes of this article, however, we’ll use Unified Modeling Language. Better known as UML, it “unifies” the popular methods into a single standard.

Simply put, an object models a physical thing or an abstract concept. Once modeled, the object can be manipulated in a natural way.

A bank account can be thought of as a simple object. You can manipulate it by making a deposit, making a withdrawal or requesting a balance statement. An object modeling a bank account would work the same way. Once modeled as an object, the bank account object could be used by a teller to deposit money into an account or by an automated statement system to retrieve a balance.

More formally, an object is an instance of a “class,” which UML defines as “a description of a set of objects that share the same attributes, operations, relationship and semantics.” In other words, a class is an object’s definition or template.

We use this definition to create objects. In a way, a class can be thought of as a blueprint or mold for creating objects. Using this definition, it is possible for each class definition to have many object instances.

The bank account object

As we said, a bank account can be represented as an object, but before we can create that object, we need its definition or class. Let’s see how we would define the class using the Java programming language.

In a text file named Account.java we would define the account class as follows:

public class Account

(open braces symbol)

// we’ll cover the guts in a moment

(closed braces symbol)

The word “public” is known as a modifier. Modifiers govern access, so “public” tells Java that anyone can create instances of the Account class.

Java defines three modifier keywords: private, protected and public. The absence of a modifier is also a fourth designation. We will cover modifiers in more depth at another time, but for now just know that modifiers govern access to the object.

The statement “class Account” tells Java that we are going to define a class named “Account.” We define the tasks that we can do to an object inside of its class definition. Java calls these actions “methods.” Our Account class will have three methods: getBalance, depositMoney and removeMoney.

A class definition also has a constructor, which we use to create new objects from the class definition. Let’s see how this changes the class definition.

public class Account

(open braces symbol)

public Account() // the constructor

(open braces symbol)

// there is nothing to do

(closed braces symbol)

public int getBalance()

(open braces symbol)

// we’ll fill this in later

(closed braces symbol)

public void depositMoney(int amount)

(open braces symbol)

// we’ll fill this in later

(closed braces symbol)

public void removeMoney(int amount)

(open braces symbol)

// we’ll fill this in soon

(closed braces symbol)

(closed braces symbol)

Each of the three method definitions above follows the same pattern: access-modifier return-type method-name(argument-list).

The return-type tells us what the method returns when we call it. The return type “int” means we expect the method to return an integer — an everyday number. The return type “void” means the method will not return anything.

You’ll also notice that some methods have an argument list in parentheses after the method name. When we call a method such as depositMoney, the argument list tells us that we have to pass the amount of money (expressed as an integer) that we want to add to the account object.

Notice that the constructor follows a slightly different pattern: access-modifier class-name(argument-list). Also notice the constructor does not have a return type; the class name acts as both the constructor name and return type.

Moving on, an object also must contain data. When we ask the bank account object to give us its balance, it has to get the number from somewhere. The object also requires numbers when we make a deposit or withdrawal. Object instances store these values in “member variables.” Data shared between all objects of the same type is stored within “class variables.”

Let’s add a member variable to our class and fill in the methods.

Public class Account

(open braces symbol)

private int balance;

// this member variable will hold account balance

public Account()

(open braces symbol)

balance = 0; // set initial balance to 0

(closed braces symbol)

public int getBalance()

(open braces symbol)

return balance;

(closed braces symbol)

public void depositMoney(int amount)

(open braces symbol)

balance = balance + amount;

// add the amount to previous balance

(closed braces symbol)

public void removeMoney(int amount)

(open braces symbol)

if(amount (less than)= balance)

// don’t allow an overdraft!

(open braces symbol)

balance = balance – amount;

// subtract the amount from the balance

(closed braces symbol)

(closed braces symbol)

(closed braces symbol)

Member variables follow a similar pattern to methods: access-modifier type variable-name.

Here we chose to make the member variable private so only the object itself can modify the value. This way, a third party can’t come along and make changes to the balance. This use of encapsulation frees users of the account object from having to know how the account object stores the balance.

You probably noticed we inserted comments throughout the code. Comments allow the programmer to interject thoughts and reminders directly into the source code to help other programmers in understanding what the code does. Comments are prefixed by // or placed inside a block designated by /* and */.

Using a class

Now we have defined a class, but how do we use it?

A class is the definition — or template — of an object. When an object is created from this template, we call that object an “instance” of the class. The instance will have all of the behavior and attributes defined by the class. In Java, you create an instance by using the “new” keyword.

Every Java program starts with a “main” method, which must appear inside of a class definition. Let’s create an account object inside of a main method, which we’ll add to the account definition:

public static void main(String args)

(open braces symbol)

Account a = new Account();

// create the account instance

a. depositMoney(1000);

// put some money into the account

int balance = a.getBalance();

// get the balance

System.out.println(“The account balance is: $” +

balance); // print balance

a.removeMoney(500); // make withdrawal

balance = a.getBalance(); // get new balance

System.out.println(“The account balance is: $” +

balance); // print the balance

(closed braces symbol)

When we run this program, we will see the following on the command line:

(greater than) The account balance is: $1000

(greater than) The account balance is: $500

More Java details

Before you can run a Java program or use Java classes, you must compile the classes. Sun’s Java JDK (see Resources) includes a Java compiler and Java virtual machine. All compiled Java programs run inside of the Java virtual machine.

To compile the account class, install the Java JDK and go to the directory that contains the file Account.java and type: javac Account.java at the command line.

After running the compiler, you will find a file named Account.class in the directory. This is the class bytecode, which runs on the Java virtual machine. Because the Account class definition defined a main method, you can run it. To run the program simply type java Account at the command line.

This is a good time to introduce the Java “classpath,” which tells the Java virtual machine where the class bytecodes live on your file system, similar to a regular path. The virtual machine uses the classpath to find class definitions, so to run the Account main properly, you must be sure that the Account class is in your classpath.

There you have it! You now know what an object is and how to write its class definitions in Java. In future installments we will expand on the ideas that we have presented here.

———-

David Hoag is vice president-development and chief object guru for ObjectWave, a Chicago-based object-oriented software engineering firm. Anthony Sintes is a Sun Certified Java Developer and team member specializing in telecommunications consulting for ObjectWave. Contact them at hob@objectwave.com or visit their Web site at www.objectwave.com.

Additional Resources

Hooked on Objects online: http://chicagotribune.com/go/objects

“The Unified Modeling Language User Guide,” Grady Booch, James Rumbaugh, Ivar Jacobson (Addison-Wesley, $47.95)

To get Sun’s Java JDK: http://www.javasoft.com/