Java Reference
In-Depth Information
4.1 Classes and Objects Revisited
In Chapter 1, we introduced basic object-oriented concepts, including a brief
overview of objects and classes. In Chapter 3, we used several predefined classes
from the Java standard class library to create objects and use them for the par-
ticular functionality they provided.
In this chapter, we turn our attention to writing our own classes. Although
existing class libraries provide many useful classes, the essence of object-oriented
program development is the process of designing and implementing our own
classes to suit our specific needs.
Recall the basic relationship between an object and a class: a class is a blueprint
of an object. The class represents the concept of an object, and any object created
from that class is a realization of that concept.
For example, from Chapter 3 we know that the String class represents a
concept of a character string, and that each String object represents a particular
string that contains specific characters.
Let's consider another example. Suppose a class called Student represents a
student at a university. An object created from the Student class would repre-
sent a particular student. The Student class represents the general concept of
a student, and every object created from that class represents an actual student
attending the school. In a system that helps manage the business of a university,
we would have one Student class and thousands of Student objects.
Recall that an object has a state , which is defined by the values of the attributes
associated with that object. The attributes of a student may include the student's
name, address, major, and grade point average. The Student class establishes that
each student has these attributes. Each Student object stores the values of these
attributes for a particular student. In Java, an object's attributes are defined by
variables declared within a class.
An object also has behaviors , which are defined by the operations associated with
that object. The operations of a student would include the ability to update that stu-
dent's address and compute that student's current grade point average. The Student
class defines the operations, such as the details of how a grade point average is com-
puted. These operations can then be executed on (or by) a particular Student object.
Note that the behaviors of an object may modify the state of that object. In Java, an
object's operations are defined by methods declared within a class.
Figure 4.1 lists some examples of classes, with some attributes and operations
that might be defined for objects of those classes. It's up to the program designer
to determine what attributes and operations are needed, which depends on the
purpose of the program and the role a particular object plays in that purpose.
Consider other attributes and operations you might include for these examples.
 
Search WWH ::




Custom Search