Beginning Java Objects: Chapter 3
Objects and Classes
Classes
A class is an abstraction describing the common features of all members in a group of similar objects.
For example, a class called 'Student' could be used to describe all student objects recognized by the
Student Registration System.
A class defines:
- The data structure (names and types of attributes) needed to define an object belonging to that
class.
- The operations (methods) to be performed by such objects: specifically, what these operations
are, how an object belonging to that class is formally called upon to perform them, and what
'behind the scenes' things an object has to do to actually carry them out.
For example, the Student class might be defined to have the following nine attributes:
which means that each and every Student object must
have these same nine attributes. Note that many of the attributes
can be represented by data types built into a programming
language (e.g. String, Float, and
Date) but that a few — advisor, courseLoad,
and transcript — are too complex for a
built-in data type to handle; we'll learn how to tackle such
attributes a bit later on.
In terms of operations, the Student class might define five methods as follows:
registerForCourse
dropCourse
chooseMajor
changeAdvisor
printTranscript
Note that an object can only do those things for which methods have been defined by the object's class.
In that respect, an object is like an appliance: it can do whatever it was designed to do (a VCR provides
buttons to play, pause, stop, rewind, and record movies), and nothing more (you cannot ask a VCR to
toast a bagel — at least not with much chance of success!). So, an important aspect of successfully
designing an object is making sure to anticipate all of the behaviors it will need in order to carry out its
'mission' within the system. We'll learn how to determine what an object's mission, data structure, and
behaviors should be, based on the requirements for a system, in Part 2 of the book.
The term feature is used to refer interchangeably to either an attribute or a method of a class. Features
are the building blocks of a class: virtually everything found within a class definition is either an
attribute or a method of the class.
In the Java language, several other types of things wind up getting included within the boundaries of
a class definition, but we won't worry about these until we get to Part 3 of the book. Conceptually,
it is perfectly appropriate to think of an object as consisting only of attributes and methods at this
point in time.
A Note Regarding Naming Conventions
It is recommended practice to name classes starting with an upper case letter, but to use mixed case for
the name overall: Student, Course, Professor, and so on. When the name of a class would ideally
be stated as a multi-word phrase, such as 'course catalog', start each word with a capital letter, and
concatenate the words without using spaces, dashes, or underscores to separate them: for example,
'CourseCatalog'.
Method and attribute names use the same mixed-case concatenation convention, except that they start
with a lower case letter. Example method names could be 'getName' and 'registerForCourse',
with 'name', 'studentId', or 'courseLoad' as attribute names.
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.