TopDefining New ClassesOverloaded methods

Overloaded methods

We also discussed overloaded methods. For example, all of our geometric objects have two versions of the moveTo method:

    public void moveTo(double x, double y) { ... }
    public void moveTo(Location point) { ... }

If a particular method name occurs more than once in a class we say that the method is overloaded. Usually we will avoid doing this unless it is clear that the methods should do the same thing, but it is convenient to allow both kinds of parameters.

We can assure that two overloaded methods do the same thing by making sure they execute the same code. Suppose we wrote out the complete definition of the first version of the moveTo method. Then we could write the second version quite simply as:

    public void moveTo(Location point) {
        this.moveTo(point.getX(), point.getY());
    }

TopDefining New ClassesOverloaded methods