<< Chapter < Page Chapter >> Page >

Calling a method on the object

Assume that the definition of the Radio class defines a method with the following format (also assume that this method is intended to simulate pressing a frequency-selector button on the front of the radio) :

public void playStation(int stationNumber)

What does this mean?

Generally, in our radio-object context, this format implies that the behavior of the method named playStation will cause the specific station identified by an integer value passed as stationNumber to be selected for play.

Public and void

The void return type means that the method doesn't return a value.

The public modifier means that the button can be pressed by anyone in the car who can reach it.

(Car radios don't have frequency-selector buttons corresponding to the private modifier in Java.)

The method signature

Continuing with our exposure of jargon, some authors would say that the following constitutes the method signature for the method identified above:

playStation(int stationNumber)

A little more Java code

Listing 2 shows the code from the earlier listing, expanded to cause the method named playStation to be called.

Listing 2 . Calling the playStation method.
Radio myObjRef = new Radio(); myObjRef.playStation(3);

The first statement in Listing 2 is a repeat of the statement from the earlier listing. It is repeated here simply to maintain continuity.

Method invocation syntax

The second statement in Listing 2 is new.

This statement shows the syntax used to send a message to a Java object, or to call a method on that object (depending on whether you prefer OOP-speak or Java-speak) .

Join the method name to the reference

The syntax required to call a method on a Java object joins the name of the method to the object's reference, using a period as the joining operator.

(In this case, the object's reference is stored in the reference variable named myObjRef . However, there are cases where an object's reference may be created and used in the same expression without storing it in a reference variable. We often refer to such an object as an anonymous object.)

Pressing a radio button

Given the previous discussion, the numeric value 3, passed to the method when it is called, simulates the pressing of the third button on the front of the radio (or the fourth button if you elect to number your buttons 0, 1, 2, 3, 4, 5) .

Summary

This is the first in a miniseries of modules that describe and discuss the necessary and most significant (essential) aspects of OOP using Java.

In order to understand OOP, you need to understand the following three concepts:

  • Encapsulation
  • Inheritance
  • Polymorphism

This module has concentrated on encapsulation. Encapsulation was used as a springboard for a discussion of objects.

A description of an object-oriented program was provided, along with a description of an object, and how it relates to encapsulation.

In order to relate object-oriented programming to the real world, a car radio was used to illustrate and discuss several aspects of software objects.

You learned that car radios, as well as software objects, have the ability to store data, along with the ability to modify or manipulate that data.

You learned that car radios, as well as software objects, have the ability to accept messages and to perform an action, modify their state, return a value, or some combination of the above.

You learned some of the jargon used in OOP, including persistence, state, messages, methods, and behaviors.

You learned where objects come from, and you learned that a class is a set of plans that can be used to construct objects. You learned that a Java object is an instance of a class.

You saw a little bit of Java code, used to create an object, and then to send a message to that object (call a method on the object).

You learned about Java references and reference variables. You learned a little about memory allocation for objects and variables in Java.

What's next?

The next module in the miniseries will introduce you to the java class.

Continuing with the real-world example introduced in this module, the next module will provide a complete Java program that simulates the manufacture and use of a car radio.

Along the way, you will see examples of (or read about) class definitions, constructing objects, saving references to objects, setter methods, sending messages to objects, instance variables and methods, class variables, array objects, persistence, and objects performing actions.

Miscellaneous

This section contains a variety of miscellaneous information.

Housekeeping material
  • Module name: Objects and Encapsulation
  • File: Jbs1010.htm
  • Published: 08/12/14
  • Revised: 09/28/15
Disclaimers:

Financial : Although the openstax CNX site makes it possible for you to download a PDF file for the collection that contains thismodule at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should beaware that some of the HTML elements in this module may not translate well into PDF.

You also need to know that Prof. Baldwin receives no financial compensation from openstax CNX even if you purchase the PDF version of the collection.

In the past, unknown individuals have copied Prof. Baldwin's modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing Prof. Baldwin as the author.Prof. Baldwin neither receives compensation for those sales nor does he know who doesreceive compensation. If you purchase such a book, please be aware that it is a copy of a collection that is freelyavailable on openstax CNX and that it was made and published without the prior knowledge of Prof. Baldwin.

Affiliation : Prof. Baldwin is a professor of Computer Information Technology at Austin Community College in Austin, TX.

-end-

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