next up previous
Next: Abstraction on objects Up: Unit 03 Previous: Lifetime of local variables

Overloading of methods

As said, Java distinguishes methods based on their whole signature, and not only on their name. Hence, we can define in the same class more than one method with the same name, as long as these methods differ in the number or type of their formal parameters (note: the name of the formal parameters is not relevant for the distinction). This feature is called overloading of methods.

Example:

public class Greetings2 {

  public static void printGreeting() {
    System.out.println("Hello!");
  }

  public static void printGreeting(String name) {
    System.out.println("Hello " + name + "!");
  }
}


next up previous
Next: Abstraction on objects Up: Unit 03 Previous: Lifetime of local variables