Things from Chapter 4 we have covered using examples
- State
of objects is the current contents. What is included in an object’s state
is defined with the instance variables
- Each
object has its own state – it’s own versions of the instance variables,
with it’s own values for them.
- Behavior
of objects is what can be done to or by an object. What behaviors are
available is based on what methods are defined for the class.
- Instance
variables can be declared anywhere in a class, but are usually defined in
the beginning
- Constructor
methods create an object of a given class. They have no return type, their
name is the same as the class name. There may be several versions with
different parameters – i.e. constructors may be “overloaded”.
- When a
given method name is used for more than one method in a class, then that
method name is “overloaded” (much as + is overloaded to mean
both addition and concatenation)
- Inspector
methods (also called accessors / getters) report on the current content
(state) of a given object
- Mutator
methods (also called setters) change the content (state) of a given object
- All
calls to normal (non-static) methods are done with
object.method(parameters)
- When a
method is called (or invoked), the program’s flow jumps directly to
the method and starts following the instructions in the method. When the method is complete, either
because of a return statement or because the end of the method is reached,
the flow returns to exactly the same place where the method was called and
continues from there. Any value passed back acts as if it replaces the
method call (for that instant (not permanently))
- The
method header includes the visibility (public/private), then the return
type, then the name of the method, then (in parentheses) the parameters –
each with a type and a name). This listing of parameters is the “formal
parameters”.
- When
calling a method, the types of parameters are not included, only the value
or variable containing the value.
The list of parameters in the call is the “actual parameters.” The values are passed
based on corresponding positions – first to first, second to second,
etc. Actually a copy of the value
is passed, but the implication of that has not arisen yet. If the actual and formal parameters do
not match, then the compiler complains.
If a method is overloaded, the system determines which method to
run based on which parameters match.
- The
method body contains the instructions of what to do – how to
carry out the behavior. It can contain any kind of statement we have
talked about – if’s, whiles, method calls, etc.
Things which came up which are actually from Chapter 5:
- “this”
is used in a method to refer to the current object – the object for/on
which the method was called.
Things we covered in chapter 2 which are now more concrete:
- Encapsulation
– an object’s data (when properly declared as private) is encapsulated
inside the object; users of the class (programmers) cannot make use of the
data directly, but must go through the methods provided with the
class. This provides the developer
of the class flexibility, in that they can change the instance variables
without impacting the user of the class.
This also provides safety – the developer can ensure that they are
in control of all changes to state, and therefore can ensure that all
changes to state are ok, reasonable, not resulting in a invalid,
illogical, … state.
Still to be covered:
- The scope
of a variable is the area within the program in which it can be used.
Private instance variables can be used anywhere within the class, but
nowhere else. Variables declared
within a method have a scope of just that method – they are considered “local”
to the method, and can only be used in that method. This is true of variables declared in
main, which is really just a method (a kind of special one). Variables declared within a block ({}’s
within an if or a while, etc ) have a scope of that block, and are only
known within that block.
- Visibility
modifiers – you have same them, but we have not used this term.
Private, protected, public are visibility modifiers. We will ignore
protected, since it involves inheritance, which we have not covered yet.
Public aspects of the class (methods usually) are accessible anywhere.
Private aspects of the class (instance variables, occasionally some methods)
are only accessible within the class. Making instance variables public
would violate encapsulation.
Occasionally methods are declared as private if they are created to
help the public methods do their job, and are not planned to be used by
users (clients) of the class.
These methods are sometimes called “support”
methods. Anything declared without
a visibility modifier have “default” visibility and can be accessed
from any class in the same package.
- A
method that does not need to return any info to the caller has a return
type of void. The return statement has nothing between return and
the semicolon.
- The signature
of a method is the method name, plus the number, type and order of
parameters (but NOT the return type). The compiler determines which version
of an overloaded method is to be used based on the signatures.
- When a
method, as part of its code, calls another method we consider this method
decomposition – the method has been broken down (decomposed) into
parts, and part of the capability is provided by another method. E.g. When
transfer method called deposit and withdraw.