Back to Main Page | News | Features | Gallery | Columns | Hands-On | Backscatter | Resources

21 May 2004

Objects and Java, Part 1

Rocky Wenz, Research Projects Coordinator, MAST
sas.z.thepilgrim@xoxy.net

Introduction

"On a bicycle, the human being can ... travel more efficiently than any other land animal on earth. Steve [Jobs] later commented that the computer had the potential to be for the mind what the bicycle is for the body." Christopher Warnoc www.ebrary.com/company/history.jsp

Object-Orientation

Bicycles for the mind! Now there is a mind picture that most anyone can understand.  Bruce Eckel, the author of Thinking in Java, takes this idea a bit further.  He says that they are mind amplification tools and a different kind of expressive medium. Object-oriented programming is part of this movement toward using the computer as an expressive medium.  In this column, I will introduce you to object-oriented programming.

But wait!  What has this to do with Java?  Object-oriented programming is the paradigm that is required by Java.  It is the way of thinking about a task that is to be done by the program written in Java.  This is how a task is broken up into units that are small enough for Java (and the person programming it) to do.

Object-oriented programming comes closer to describing the real world than many other types of programming because it has a simple rule at its core.  This rule also describes much of the real world.  This rule is: Everything is an object, and often objects are made of smaller objects.  Java enforces this rule strongly.

All objects have a current condition or mode that is called its state.  The state of an object can change often. An object in Java has three aspects:  It contains information (programmers call it data) and this data gives it a current state.  It has specific behavior (called methods in Java, this is what the object can do.)  Finally, it has a unique identity, which is usually a name, but may only be a unique address in the memory of the computer.  In theory, you can take any conceptual component in the problem you are trying to solve and represent it as an object in Java.

So how do we get an object in Java to do something for us?  We do this through its interface. This is the way that we access the methods or, as they say in Java, send a message to the object.  Each object in Java has a finite number of specific requests it can fulfill.  There is a way to represent this, called the Unified Modeling Language (UML).  Let's take a lightbulb as a simple example.

[Graphics:HTMLFiles/index_1.gif]

In UML, each object is represented by a box.  The name of the object is located in the top portion of the box.  Any data that is contained in the object that you wish to describe is placed in a middle portion.  Such a middle portion is optional, and is not included above. 

Lastly, any methods this object has are listed in the bottom portion of the box. For the lightbulb we have the methods of on (activation), off (deactivation), brighten (intermediate state between on and off), and dim (another intermediate state between on and off.)  This last portion is also optional if you are only interested in the object's name and/or data.

So our lightbulb example has an interface with 4 methods. The pair of parentheses after each method's name is there for reasons that will become clear later.

Next time: Class and Inheritance.

Exercises

1. Look around and find an object. Think about the smaller objects from which it is made. Then think about each of these objects.  Are they made of still smaller objects?  If so, continue to think of the objects these are made of until you can't divide any objects into other objects. Then start again with another object.

2. Choose one of the objects from Exercise 1.  Create a UML diagram for it.

3. Think about what you described in Exercise 2 of the Introduction.  Consider it as an object. Give it a name. What methods will it need to have to create the behavior that you wish it to have?  What data (states) will it need to support those methods?  Don't worry if you can't come up with a complete list of these things at this time.


Created by Mathematica  (April 22, 2004).

Contact Us | Privacy Statement | SAS Home Page
Copyright 2004 by Society for Amateur Scientists