Sunday, November 20, 2011

Class Definition » Method Overloading

Method Overloading
Java allows you to have multiple methods having the same name, as long as each method
accept different sets of argument types. In other words, in our example, it is legal to have
these two methods in the same class.
public String printString(String string)
public String printString(String string, int offset)
This technique is called method overloading. The return value of the method is not taken
into consideration. As such, these two methods must not exist in the same class:
public int countRows(int number);
public String countRows(int number);

Using Method Overloading
A method's name with the types and sequence of the parameters form the method's signature
public class MainClass {

  public void print(int a) {
    System.out.println(a);
  }

  public void print(String a) {
    System.out.println(a);
  }

}
Pass long parameters to overloading method
It is legal to have these two methods in the same class.
public class MainClass {
  public int printNumber(int i) {
      return i*2;
  }

  public long printNumber(long i) {
      return i*3;
  }
  public static void main(String[] args) {

  }

}
printNumber(3) will invoke this method:
public int printNumber(int i)
To call the second, pass a long:
printNumber(3L);

Primitives and overloading
public class MainClass {
  void f1(char x) {
    System.out.println("f1(char)");
  }

  void f1(byte x) {
    System.out.println("f1(byte)");
  }

  void f1(short x) {
    System.out.println("f1(short)");
  }

  void f1(int x) {
    System.out.println("f1(int)");
  }

  void f1(long x) {
    System.out.println("f1(long)");
  }

  void f1(float x) {
    System.out.println("f1(float)");
  }

  void f1(double x) {
    System.out.println("f1(double)");
  }

  void f2(char x) {
    System.out.println("f2(char)");
  }

  void f2(byte x) {
    System.out.println("f2(byte)");
  }

  void f2(short x) {
    System.out.println("f2(short)");
  }

  void f2(int x) {
    System.out.println("f2(int)");
  }

  void f2(long x) {
    System.out.println("f2(long)");
  }

  void f2(float x) {
    System.out.println("f2(float)");
  }

  void f3(char x) {
    System.out.println("f3(char)");
  }

  void f3(byte x) {
    System.out.println("f3(byte)");
  }

  void f3(short x) {
    System.out.println("f3(short)");
  }

  void f3(int x) {
    System.out.println("f3(int)");
  }

  void f3(long x) {
    System.out.println("f3(long)");
  }

  void f4(char x) {
    System.out.println("f4(char)");
  }

  void f4(byte x) {
    System.out.println("f4(byte)");
  }

  void f4(short x) {
    System.out.println("f4(short)");
  }

  void f4(int x) {
    System.out.println("f4(int)");
  }

  void f5(char x) {
    System.out.println("f5(char)");
  }

  void f5(byte x) {
    System.out.println("f5(byte)");
  }

  void f5(short x) {
    System.out.println("f5(short)");
  }

  void f6(char x) {
    System.out.println("f6(char)");
  }

  void f6(byte x) {
    System.out.println("f6(byte)");
  }

  void f7(char x) {
    System.out.println("f7(char)");
  }

  void testDouble() {
    double x = 0;
    System.out.println("double argument:");
    f1(x);
    f2((floatx);
    f3((longx);
    f4((intx);
    f5((shortx);
    f6((bytex);
    f7((charx);
  }

  public static void main(String[] args) {
    MainClass p = new MainClass();
    p.testDouble();
  }
}


http://www.java2s.com/Tutorial/Java/0100__
Class-Definition/Catalog0100__Class-Definition.htm

No comments:

Post a Comment