Consider a modified version of the class Circle:
import java.lang.Math;
public class Circle
{
// Accessor methods
public double x() { return m_x; }
public double y() { return m_y; }
public double radius() { return m_r; }
// Mutator methods
public void x(double v) { m_x = v; }
public void y(double v) { m_y = v; }
public void radius(double r) { m_r = r; }
// Operations/Attributes
public double getCircumference()
{ return 2 * Math.PI * m_r; }
public double getArea()
{ return Math.PI * m_r*m_r; }
// Data
double m_x, m_y; // coordinates of center
double m_r; // radius
}
where I’ve changed the accessor and mutator method names for illustrative purposes.
We now have two methods named x, two methods named y, and two methods named radius. Note that they have different signatures — that is, they have different sets of arguments.
Creating multiple methods with the same name, but with different signatures, is known as method overloading.
Note: The Java standard naming convention is to use
getFooandsetFooas names for the accessor and mutator methods for an attribute namedfoo. I strongly recommend that you follow this convention in all your Java code. (Note that the Sun Java programmers did not initially follow this convention in early Java classes, but they do in all current code.)
