<< Chapter < Page Chapter >> Page >

Sending a message to the object

Using typical OOP jargon, the statement in Listing 3 sends a message to the Radio object, asking it to change its state according to the values passed as parameters.

Pressing a button on the radio

Finally, the code in Listing 4 calls the method named playStation on the Radio object, passing the integer value 3 (the button number) as a parameter.

Listing 4 . Pressing a button on the radio.
myObjRef.playStation(3);

Another message

This code sends a message to the object asking it to perform an action. In this case, the action requested by the message is:

  • Tune yourself to the frequency previously associated with button number 3
  • Play the radio station that you find at that frequency through the speakers

How does this simulated radio play?

This simple program doesn't actually play music. As you will see later, this causes the message shown in Figure 1 to appear on the computer screen, simulating the selection and playing of a specific radio station.

Figure 1 . Screen output.
Playing the station at 93.5 Mhz

The Radio class

Listing 5 shows the class definition for the Radio class in its entirety.

Listing 5 . The Radio class .
class Radio{ //This class simulates the plans from// which the radio object is created. protected double[]stationNumber = new double[5];public void setStationNumber( int index,double freq){stationNumber[index] = freq;}//end method setStationNumberpublic void playStation(int index){ System.out.println("Playing the station at " + stationNumber[index]+ " Mhz"); }//end method playStation}//end class Radio

Note that the code in Listing 5 does not contain an explicit constructor. If you don't define a constructor when you define a new class, a default version of the constructor is provided on your behalf. That is the case for this simple program.

(Constructors will be explained in detail in subsequent modules.)

The plans for an object

The code in Listing 5 provides the plans from which one or more objects that simulate physical radios can be constructed.

An object instantiated (an object is an instance of a class) from the code in Listing 5 simulates a physical radio. I will subdivide this code into fragments and discuss it in the following listings.

An instance variable

In an earlier module, I explained that we often say that an object is an instance of a class. (A physical radio is one instance of the plans used to produce it.) The code in Listing 6 shows the declaration and initialization of what is commonly referred to as an instance variable.

Listing 6 . An instance variable.
protected double[] stationNumber =new double[5];

Why call it an instance variable?

The name instance variable comes from the fact that every instance of the class (object) has one. (Every radio produced from the same set of plans has the ability to associate a frequency with each selector button on the front of the radio.)

Class variables - an aside

Note that Java also supports something called a class variable , which is different from an instance variable .

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Accessible objected-oriented programming concepts for blind students using java. OpenStax CNX. Sep 01, 2014 Download for free at https://legacy.cnx.org/content/col11349/1.17
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Accessible objected-oriented programming concepts for blind students using java' conversation and receive update notifications?

Ask