Chapter 7: Introduction to Objects


The previous chapter presented arrays, which are Java's simplest kinds of objects. Arrays are much less sophisticated than other kinds of objects. Usually when people say "object," they mean it in a casual sense that excludes arrays. A program whose only objects are arrays can hardly be called object-oriented. However, in learning about arrays, you have learned a number of concepts that are vital to your mastery of full-fledged objects. You are now ready to enter the world of object-oriented programming, perhaps never to return.

The animated illustrations for this chapter provide visual reinforcement for the concepts that will be presented here. Please be sure to run them and take the time to play with them when the text invites you to do so.

Arrays Versus Objects

Before we begin, let's agree on some terminology. In the most formal sense, an array is a kind of object. However, we are about to compare arrays and other objects, and we need to avoid cumbersome language. It would be useful to say, for example, "objects have data and methods, rather than, "objects that aren't arrays have data and methods." So for the remainder of this book, unless it will cause confusion, "object" will mean "object but not array."

You already know a lot about objects from your study of arrays. Here are some similarities between arrays and objects:

  • Objects contain clusters of data.

  • Objects are created by invoking the keyword new.

  • Objects inhabit inaccessible memory.

  • Objects are manipulated indirectly, via references.

  • Object references can be passed as method arguments; objects cannot.

  • Objects are not explicitly destroyed; they are garbage-collected when they have no more references.

Another recognizable feature of objects is the use of the period as a symbol to denote "property of" when it follows the name of an array or object. With arrays, the syntax arrayReference.length gave you the number of components in the array. With objects, the syntax objectReference.something gives you access to the extensive power and features of an object. You will see how this works in great detail later in this chapter.

Objects have many features that go far beyond what arrays can do. Here are some unique features of objects:

  • They can contain data of different types.

  • They can contain methods as well as data.

  • They are related to classes.

Classes are among the most important concepts in object-oriented programming. They are actually quite simple to understand, as you will see in the next section.




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

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