What Is a Message?

A single object alone is generally not very useful. Instead, an object usually appears as a component of a larger program or application that contains many other objects. Through the interaction of these objects, programmers achieve higher-order functionality and more complex behavior. Your bicycle hanging from a hook in the garage is just a bunch of titanium alloy and rubber; by itself, the bicycle is incapable of any activity. The bicycle is useful only when another object (you) interacts with it (pedal).

Software objects interact and communicate with each other by sending messages to each other. When object A wants object B to perform one of B's methods, object A sends a message to object B (Figure 30).

Figure 30. Objects interact by sending each other messages.

graphics/02fig03.gif

Sometimes, the receiving object needs more information so that it knows exactly what to do; for example, when you want to change gears on your bicycle, you have to indicate which gear you want. This information is passed along with the message as parameters.

Figure 31 shows the three components that comprise a message:

Figure 31. Messages use parameters to pass along extra information that the object needsin this case, which gear the bicycle should be in.

graphics/02fig04.gif

  • The object to which the message is addressed (YourBicycle)
  • The name of the method to perform (changeGears)
  • Any parameters needed by the method (lowerGear)

These three components are enough information for the receiving object to perform the desired method. No other information or context is required.

Messages provide two important benefits.

  • An object's behavior is expressed through its methods, so (aside from direct variable access) message passing supports all possible interactions between objects.
  • Objects don't need to be in the same process or even on the same machine to send and receive messages back and forth to each other.

Getting Started

Object-Oriented Programming Concepts

Language Basics

Object Basics and Simple Data Objects

Classes and Inheritance

Interfaces and Packages

Handling Errors Using Exceptions

Threads: Doing Two or More Tasks at Once

I/O: Reading and Writing

User Interfaces That Swing

Appendix A. Common Problems and Their Solutions

Appendix B. Internet-Ready Applets

Appendix C. Collections

Appendix D. Deprecated Thread Methods

Appendix E. Reference

show all menu





The Java Tutorial(c) A Short Course on the Basics
The Java Tutorial: A Short Course on the Basics, 4th Edition
ISBN: 0321334205
EAN: 2147483647
Year: 2002
Pages: 125
Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net