Beginning Java Objects: Chapter 3 Objects and Classes
Composite Classes
Whenever we have a class, such as Student or
Professor, in which one or more of the attributes are
themselves references to other objects, we refer to the class as a
composite class. The number of levels to which objects can be
conceptually bundled inside one another is endless, and so composite
classes enable us to model very sophisticated real world concepts. As it
turns out, most 'interesting' classes are composite classes.
With composite class instances, it appears as though we are nesting objects one inside the other:
Object nesting does indeed sometimes make sense: namely, if an object 'A'
doesn't have a life of its own, and only exists for the purpose of serving
enclosing object 'B'.
- Think of your brain, for example, as an object that exists only within the context of your body
(another object).
- As an example of object nesting relevant to the SRS, let's consider a
grade book used to track student performance in a particular course. If we
were to define a
GradeBook class, and then create
GradeBook objects as attributes — one per
Course object — then it might be reasonable for each
GradeBook object to exist wholly within its associated
Course object. No other objects would need to communicate with
the GradeBook directly; if a Student object
wished to ask a Course object what grade the
Student has earned, the Course object might
internally consult its embedded GradeBook object, and simply
hand a letter grade back to the Student.
However, as we saw in the sample Student and
Professor classes above, we often encounter a situation where
an object 'A' needs to refer to an object 'B', object 'B' needs to refer
back to 'A', and both objects need to be able to respond to requests
independently of each other. In such a case, we cannot physically embed one
object wholly inside the other; this would lead to infinitely recursive
nesting.
Handles come to the rescue! In reality, we are typically not storing whole
objects as attributes inside of other objects, but rather references
to objects. When an attribute of an object 'A' is defined in terms of an
object reference 'B', the two objects exist separately in memory, and
simply have a convenient way of finding one another whenever it is
necessary for them to interact. Think of yourself as an object, and your
cellular phone number as your handle. Other people — 'objects'
— can reach you to speak with you whenever they need to, even though
they don't know where you are physically located, using your phone number.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|