7.5. Overloading

As in C++ and other object-oriented languages, Java allows more than one method with the same name but with different behaviors, depending on the type or number of its arguments. For instance, you could define two isBig methods: one that determines if a String is “big” (by some arbitrary measure) and another that determines if an int is “big,” as follows:

public boolean isBig(String s) {
  return(s.length() > 10);
}

public boolean isBig(int n) {
  return(n > 1000);
}

Note that

return(n > 1000);

is a more compact way of accomplishing the same thing as

if (n > 1000) {
  return(true);
} else {
  return(false);
}

In Listing 7.4, the Ship4 constructor and the move method are overloaded. One constructor can call another constructor in ...

Get Core Web Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.