Logout

Home OOP Option Last Next

D.3.2

Define the terms: method, accessor, mutator, constructor, signature, return value.

 

Teaching Note:

These are generally related to the object’s actions. See JETS.

 

Sample Question:

sdfsdfsf

JSR Notes:


Definitions:

method - a discrete block of code which specializes in accomplishing a certain task

accessor - a method which specializes in returning a private variable of an object

mutator - a method which specializes in changing in some way a private variable of an object

constructor - a special method which "constructs" an instance of a certain class. It generally takes in specific parameters which set the private attributes to certain starting values.

signature - the specific nature of a method's header - so,

return value - a value which is returned to where a method is called. The method call therefore evaluates to the return value. The return value may be void, in which case the method accomplishes its task(s) without returning anything.


More Details:

method - a discrete block of code which specializes in accomplishing a certain task.
You should have a pretty good idea of what methods look like by now; most code is grouped into methods, in the case of Java in a block of code held in curly braces { }. In fact, classes of code are basically attributes and methods which act on those attributes. The word "function" can be used interchangeably with "method".

accessor - a method which specializes in returning a private variable of an object.
To this point, in our template classes, we have called these "get methods". Here's a quick example:

public String getColor(){
    return color;
}


mutator
- a method which specializes in changing in some way a private variable of an object.
To this point, in our template classes, we have called these "set methods". Here's a quick example:

public void setColor(String color){
    this.color = color;
}


constructor
- a special method which "constructs" an instance of a certain class. It generally takes in specific parameters which set the private attributes to certain starting values.
As we've seen, a "default" constructor takes in no parameters, and so an instance made with it will just take the default values of the class. Otherwise, that's a purpose of the constructor, to take in and set initial values of the instance's properties.

Remember that the constructor method is special in two ways: 1. it is exactly the name of the class, and 2. it has no return type, not even void. Here's a quick example of a constructor that takes in two parameters:

public Marker(int length, String color){
    this.length = length;
    this.color = color;
}


signature
- the specific nature of a method's header - so,

It is worth noting that the signature makes up what is referred to as the "public interface" of the method. It is what (and is all that) other classes can see of the method; its contents are not accessible.


return value
- a value which is returned to where a method is called. The method call therefore evaluates to the return value. The return value may be void, in which case the method accomplishes its task(s) without returning anything.
Remember that there are three general situations for return values: 1. there is none, in the case of constructors, 2. the return value exists, but it is void, i.e. nothing, and 3. the return value is some sort of primitive or object value.
Here is a quick example of a method which returns a value (a primitive double value in this case).:

public double addTwoNumbers(double a, double b){
    double answer = a + b;
    return answer;
}