Chapter 4
Section 4.1
- objects have a state of
being and a behavior – these are their variables and methods
- each object has its own
state, but in general we can describe any object’s behavior
- state often modifies
its behavior
- objects are defined by
classes
- class is model or
blueprint of object – describes types of data object will hold and code
for its methods
- no data space is
reserved in a class – we have to instantiate one or more objects of the
class, then each object will have its own space for data
Section 4.2
- class has any number of
data declarations and methods, called the members of the class
- constructor has the same name as the class, and
gets called with new is used to create a new instance of that class
- listing 4.1, 4.2, p.
179, 180
- scope of variable is
defined by location where it is declared – if a variable is declared in a
class, its scope includes all the methods of the class
- data declared in a
class is called instance data, since memory space is created for each
instance of the class created
- listing 4.3, p. 182
- objects should be
self-governing, so variables in an object should only be able to be
modified within that object – only methods of that object should have
access to them
- this is called encapsulation
– object should be encapsulated from rest of program
- should write methods to
allow any instance data modification – these define the interface between
the object and the rest of the program
- client shouldn’t be
able to interact with instance data directly, but use methods to do it
- visibility modifiers:
public and private
- public – can be
directly referenced from outside the object
- private – can only be
reference inside class definition
- if a method provides a
service of the class, it should be public, but many instance variables
will be declared private
- constants are public
many times, since they are final and can’t be changed anyway
Section 4.3
- every method is part of
a class, and has method declaration
- when method is called,
flow of control transfers to method, then back to where call was made
- header includes type of
return value, method name, list of parameters, then statements of body are
in { }
- listing 4.4, 4.5, p.
188, 189
- return type can be
primitive type, class name or void
- if method returns a
value, must have return in method
- constructors have no
return value ever, so no return line
- can ignore return value
when call a method, if desired
- parameter is value
passed to a method
- in method declaration,
formal parameters are listed, whereas when method is called, actual
parameters are passed
- parameters always go in
( ) after method name, and if there are no parameters, have empty ( )
- parameter list in
invocation must match that of method declaration
- constructors can also
take parameters
- constructor is called
when object is created
- it is same name as
class, and never has return value
- every class has default
constructor, if you choose not to define one
- scope of a variable is
part of program where that variable can be referenced validly – part of
the program where it is considered alive
- variable can be
declared in a method, making it local data as opposed to instance data –
its scope is only that method
- instance data has scope
of entire class – any method in class can use it
Section 4.4
- some objects are made
up of other objects, like a String object – this is has-a relationship
- methods of class can
take objects as parameters
- listing 4.6, 4.7, p.
196, 197
Section 4.5
- can overload methods –
give multiple methods the same name
- some combination of
type and number of parameters must be different to make them unique – must
have unique method signature
- println is overloaded
many times
- constructors are many
times overloaded, too
- listing 4.8. 4.9, p.
203, 204
Section 4.6
- should try to decompose
methods into multiple methods for cleaner design
- listing 4.10, 4.11, p.
207, 209
Section 4.7 – 4.8
- applets have built-in
methods
- init is called when
applet is first loaded
- start and stop are
called when it’s active or inactive
- init only called once,
but start could be called many times
- listing 4.12, 4.13, p.
212, 215