A (175)

(Tuis.) #1

134 CHAPTER 5: Introduction to Java: Objects, Methods, Classes, and Interfaces


Objects can be as complicated as you wish them to be, and Java objects can nest or contain other
Java objects within their object structure, or object hierarchy. An object hierarchy is like a tree
structure, with a main trunk, and branches, and then sub-branches as you move up (or down) its
structure, and then leaves at the ends of these branches. A good example of a hierarchy that you
use every day would be your multi-level directory or folder structure that is on your computer’s hard
disk drive (refer to Figure 1-9 back in Chapter 1, for a visual example).


Directories or folders on your hard disk drive can contain other directories or folders, which can in
turn contain yet other directories and folders, allowing complex hierarchies of organization to be
created. We saw another really great example of this back in Chapter 3, shown in Figure 3-9, where
our Android HelloUniverse project folder was shown in the Eclipse Android Development Tools (ADT)
Project Explorer pane, showing the project sub-folder hierarchy, with its source folder, and resource
folder, and sub-folders for your layouts, menus, drawables, values, and the like.


You can do this same hierarchical construction using Java objects, which can contain sub-objects,
which can themselves contain further sub-objects, and so on and so forth as needed to create an
object hierarchy structure.


You’ll see plenty of nested object hierarchies when you are working with Android, because nested
objects are very useful for “grouping” related objects that are used together, but that are often used
in more than one place, and for more than one type of usage. After all, the goal of modular code is to
foster effective code re-use!


In other words, some types of objects are similarly useful to other types of objects in an Android
app, so they are provided in a nested object hierarchy underneath the other object types. You will
see many examples of this during this book, as we will be covering all of the primary Java classes
(which as you will soon see are used to create objects) in Android in great detail during the course of
this book.


As an exercise, you should practice identifying different objects in the room around you, and then
break their definition or description down into states (variable characteristics and fixed or constant
characteristics) as well as behaviors (things that the objects can or will do). This is a good exercise
to perform, because this is how you will need to start thinking in order to become more successful
in your OOP endeavors using the Java programming language (and even using the XML markup
language, for that matter).


Figure 5-1. Car object, showing the car attributes or characteristics (inner oval) and the car behavior (outer oval)

Free download pdf