Java: Method overloading

It is possible to declare multiple methods with the same name as long as the parameters vary in type or number.

It is a powerful feature that allows a method to handle a variety of arguments without the programmer needing to be aware of using different methods.

package javaapplication18;

public class JavaApplication18 {

    static class MyApp {

        void PrintOut(String s) {
            System.out.println(s);
        }
        void PrintOut(int i) {
            System.out.println(i);
        }
    }

    public static void main(String[] args) {
        MyApp m = new MyApp();
        m.PrintOut("Hello");     
        m.PrintOut(15);
    }
}

So in the above example, we have 2 methods declared for PrintOut.  The first has a string argument and the second has an integer argument.

overloading

Series Navigation<< Java: Return statement
Java: Class >>