home—info—lects—labs—exams—hws
textbook—tutor/PIs—java.lang docs—java.util docs
lect02c
objects: state + methods
We have worked with calling String methods,
like length, replace, etc..
Today we will explore the idea that objects
are data which have two things:
-
Methods:
questions which the object can answer
-
State:
information associated with each separate object
(or “instance”)
Let's work with an example, and come back to
the concept of "object".
We'll use objects of the type java.awt.Rectangle.
-
Create a java.awt.Rectangle,
by using a special keyword “new”:
new java.awt.Rectangle( 200, 300, 40, 50 )
|
Note that BlueJ gives us back an answer:
a brand new object!
-
We'll double-click on the tiny red box,
and see that the Rectangle contains some information
inside of it.
observation:
A Rectangle object holds information -- it
is not (for a computer scientist) something which must be drawn.
Compare to String objects:
they can be useful even if we never print them on the terminal window
(we can still call their methods like length
and charAt etc.).
-
Let's create a second, different object, and inspect it.
-
Note that once we know this official name of the type,
we can create a variable which can hold a Rectangle:
let's do it!
What picture (“memory diagram”) do we draw on the board?
Remember, a variable is simply a (labeled) box that can hold
(in this case) a Rectangle.
-
Now, let's initialize our variable.
How do we modify our memory-diagram we've drawn on the board?
For the Rectangle object,
we'll draw the same 'inspector' box that BlueJ drew.
Rather than drawing the Rectangle object
inside our variable (like you'd expect),
we'll actually place an arrow (or reference)
inside the variable's box.
We'll discuss this more later
but notice that it explains why BlueJ said
the constructor returned a “Rectangle reference”.
-
Call getter methods — methods which
just ask the object to tell us some of its internal state.
Example getter methods are:
getX, getY, getWidth, …
-
Now, we'll call
some mutators: methods which tell the object
to change its internal state.
Example mutator methods are:
setSize, setLocation, and grow.
Hmm, when we call a mutator method, we don't get any answer back.
How can we tell whether the object did what we expect??
Yes — let's call some getter methods again and
verify that they are giving a different answer than before.
(Also, see the inspect-window change.)
In general, we say that an object has
state
(its internal information: in this case,
the x,y location
of its NW corner,
its width, and its height),
and
behaviors (the methods
Here are some terms for the same thing.
You will need to know that
objects = state + behaviors,
although you can use any of the rows below:
object-oriented term |
state |
behaviors |
math term |
data |
functions1 |
Java term |
fields |
methods |
Next time we'll
talk about
writing tester-programs,
and have another example of objects with state.
Friday we'll talk
about interfaces, and signatures.
Starting next Monday,
we'll actually write a program where
we make our own class of objects,
with whatever state and behaviors we want!
1More precisely, people use
the term “functions”
only for methods which return an answer (like getters)
and instead they use the term
“procedure”
to mean void methods (like mutators).
↩
home—info—lects—labs—exams—hws
textbook—tutor/PIs—java.lang docs—java.util docs