Getting Started with Java - Attributes and Behavior
(Page 6 of 6 )
A Java class consists of two distinct types of information: attributes and behavior.
Both of these are present in VolcanoRobot, a project you will implement today as a class. This project, a computer simulation of a volcanic exploration vehicle, is inspired by the Dante II robot used by NASA's Telerobotics Research program to do research inside volcanic craters.
Attributes of a Class of ObjectsAttributes are the data that differentiates one object from another. They can be used to determine the appearance, state, and other qualities of objects that belong to that class.
A volcanic exploration vehicle could have the following attributes:
Status—exploring, moving, returning home
Speed—Measured in miles per hour
Temperature—Measured in Fahrenheit degrees
In a class, attributes are defined by variables—places to store information in a computer program. Instance variables are attributes that have values that differ from one object to another.
An instance variable defines an attribute of one particular object. The object's class defines what kind of attribute it is, and each instance stores its own value for that attribute. Instance variables also are called object variables.
Each class attribute has a single corresponding variable; you change that attribute in an object by changing the value of the variable.
For example, the VolcanoRobot class could define a speed instance variable. This must be an instance variable because each robot travels at different speeds depending on the circumstances of the environment. The value of a robot's speed instance variable could be changed to make the robot move more quickly or slowly.
Instance variables can be given a value when an object is created and then stay constant throughout the life of the object. They also can be given different values as the object is used in a running program.
For other variables, it makes more sense to have one value shared by all objects of that class. These attributes are called class variables.
A class variable defines an attribute of an entire class. The variable applies to the class itself and to all its instances, so only one value is stored no matter how many objects of that class have been created.
An example of a class variable for the VolcanoRobot class would be a variable that holds the current time. If an instance variable were created to hold the time, each object could have a different value for this variable, which could cause problems if the robots are supposed to perform tasks in conjunction with each other.
Using a class variable prevents this problem because all objects of that class share the same value automatically. Each VolcanoRobot object would have access to that variable.
Behavior of a Class of ObjectsBehavior refers to the things that a class of objects can do to themselves and other objects. Behavior can be used to change the attributes of an object, receive information from other objects, and send messages to other objects asking them to perform tasks.
A volcano robot could have the following behavior:
Behavior for a class of objects is implemented using methods.
Methods are groups of related statements in a class of objects that handle a task. They are used to accomplish specific tasks on their own objects and on other objects, and they are used in the way that functions and subroutines are used in other programming languages.
Objects communicate with each other using methods. A class or an object can call methods in another class or object for many reasons, including the following:
To report a change to another object
To tell the other object to change something about itself
To ask another object to do something
For example, two volcano robots could use methods to report their locations to each other and avoid collisions, and one robot could tell another to stop so that it could pass by.
Just as there are instance and class variables, there are also instance and class methods. Instance methods, which are so common they're usually just called methods, are used when you are working with an object of the class. If a method makes a change to an individual object, it must be an instance method. Class methods apply to a class itself.
Be sure to come back next week for the next part of this article.