Java Reference
In-Depth Information
Object-oriented programming enables you to develop large-scale software and GUIs
effectively.
Key
Point
Having learned the material in the preceding chapters, you are able to solve many program-
ming problems using selections, loops, methods, and arrays. However, these Java features
are not sufficient for developing graphical user interfaces and large-scale software systems.
Suppose you want to develop a graphical user interface (GUI, pronounced
goo-ee
) as shown
in Figure 9.1. How would you program it?
why OOP?
Button
Label
Text Field
Check Box
Radio Button
Combo Box
F
IGURE
9.1
The GUI objects are created from classes.
This chapter introduces object-oriented programming, which you can use to develop GUI
and large-scale software systems.
A class defines the properties and behaviors for objects.
Key
Point
Object-oriented programming (OOP) involves programming using objects. An
object
rep-
resents an entity in the real world that can be distinctly identified. For example, a student, a
desk, a circle, a button, and even a loan can all be viewed as objects. An object has a unique
identity, state, and behavior.
VideoNote
Define classes and objects
The
state
of an object (also known as its
properties
or
attributes
) is represented by
data fields
with their current values. A circle object, for example, has a data field
radius
, which is the property that characterizes a circle. A rectangle object has the
data fields
width
and
height
, which are the properties that characterize a rectangle.
object
state of an object
properties
attributes
data fields
behavior
actions
■
The
behavior
of an object (also known as its
actions
) is defined by methods. To
invoke a method on an object is to ask the object to perform an action. For exam-
ple, you may define methods named
getArea()
and
getPerimeter()
for circle
objects. A circle object may invoke
getArea()
to return its area and
getPerim-
eter()
to return its perimeter. You may also define the
setRadius(radius)
method. A circle object can invoke this method to change its radius.
■
Objects of the same type are defined using a common class. A
class
is a template, blue-
print, or
contract
that defines what an object's data fields and methods will be. An object is an
instance of a class. You can create many instances of a class. Creating an instance is referred
to as
instantiation
. The terms
object
and
instance
are often interchangeable. The relationship
between classes and objects is analogous to that between an apple-pie recipe and apple pies:
You can make as many apple pies as you want from a single recipe. Figure 9.2 shows a class
named
Circle
and its three objects.
A Java class uses variables to define data fields and methods to define actions. Addition-
ally, a class provides methods of a special type, known as
constructors
, which are invoked to
create a new object. A constructor can perform any action, but constructors are designed to
perform initializing actions, such as initializing the data fields of objects. Figure 9.3 shows an
example of defining the class for circle objects.
class
contract
instantiation
instance
data field
method
constructors
Search WWH ::
Custom Search