A Bit More About Objects
In the last lesson we learned what an Object is. In this lesson we will explore how Objects are used.
Object communication and interaction
It is through interactions among objects that programmers get the behavior their programs were designed for. Software objects communicate and interact with each other in two ways: by calling (or invoking) each other's methods or by directly accessing their variables.
Calling an instance method:
Object A calls a method implemented by object B to have it to perform some behavior or return some value. This is also sometimes referred to as A sending a message to B. For example, when the play button is pressed on the CD player application, the button object may call the "play" method of the application object with the understanding that this means to play the current track.
Sometimes the called object's method needs additional information in order to perform its task. If your CD player application had multiple buttons, each of which played a different track, then the play method would also need the number of the track to play. This information would be passed along as an argument (or parameter) when calling the method.
There are three parts to calling a method:
- The object you are calling that implements the method (e.g., the CD app object)
- The name of the method to perform (e.g., play)
- Any arguments needed by the called object (e.g., the CD track #)
The syntax for making a method call is to list the object to be called, followed by a period, then the method name. Any arguments needed are enclosed in parentheses after the method name.
A line of Java code to have the cdApp object play track 3 might look like:
It is also very common for an object to call one of its own methods as part of the implementation of another method. For example, suppose the CD player application was playing through a play list and it was time to play the next song in the list. The application object would need to call its own play() message, passing the number of the track to play next. Code to do this would look like:
or more commonly,
The special keyword "this" always refers to the object whose code is currently executing. In the second case there is no object specified. If a method is called without an explicit object, it is assumed the object is calling one of its own methods. As you will see, this will be very common in the turtles you will write in Jurtle.
Accessing an instance variable:
Sometimes object A needs to learn something about the state of object B. Since an object's state is generally stored in instance variables, objects sometimes need access to each other's instance variables. While it is generally considered best to call a method (called an accessor method) to get information about another object's state, sometimes this is not possible and the instance variable must be accessed directly.
Accessing an instance variable is similar to calling a method. You need to specify:
- The object whose variable you want
- The name of the variable
The syntax for referencing an instance variable is to list the object called, followed by a period, then the variable name.
A line of code to get the playingStatus of the cdApp object would be:
As discussed with calling methods above, it is also very common for an object to access its own instance variables. In this case you can either use the "this" keyword, or more commonly just specify the variable by itself. Code to do this would look like:
Using static variables and static methods
Calling a static method or accessing a static variable is similar to their instance counterparts. However, where you would normally specify the instance object you instead specify the class. For example, the Color class in the java.awt package has a number of useful static variables and methods that you will be using in your turtles. To access the static variable holding the color red, you would type:
Likewise to call Color's static method that creates a Color instance using hue, saturation and brightness you would call:
Color.getHSBColor(0.5f, 1.0f, 1.0f)
[More details about using color in Java may be found in Lesson 7: Specifying Colors in Java]
Creating (or instantiating) new objects
We have talked about how to use objects, but how do you create (instantiate) them in the first place? In Java you generally create new objects of a given class by calling a special method in that class called a constructor. You do this using the new operator. To call a constructor, you type new, followed by the class name, followed by the constructor's arguments in parentheses.
For example to create a new "green" Color instance using the constructor that takes three parameters specifying the red, green and blue components you would type:
new Color(0, 255, 0)
The constructor method itself is declared in the Color class file as:
public Color(int r, int g, int b)
Constructor methods are declared a bit differently from other methods. The method name is the same as the class name, and you don't declare a return type for the method (since it always returns an instance of the class). See Lesson 12: Methods - Creation and Use for more information about declaring and using constructors.
Take your object from the previous lesson's exercise and write some practice pseudo-code using its methods and instance variables. Try to have the object do something.
For example, if you chose a Blender as your object, you might have the following variables and methods:
To make a smoothie you might write the following pseudo-code:
blender.addToContents( frozenBanana )
blender.addToContents( raspberries )
blender.addToContents( milk )
blender.setSpeed( high )
while (contentLumpiness is not smooth)
wait( 10 seconds)