Pages

Method Overloading

What is Method Overloading?

Method Overloading:
  • When a class have several Methods having same name but different in parameters/arguments, it is known as Method Overloading.
Method Overloading
Method Overloading

Demo on Method Overloading:
public class MethodOverloadingDemo {

    public void sum(int a, int b) {
        System.out.println("Sum of Integer values=" + (a + b));
    }

    public void sum(float e, float f) {
        System.out.println("Sum of float values=" + (e + f));
    }

    public int sum(int a, int b, int c) {
        System.out.print("Sum of Intergers with return type Int=");
        return (a + b + c);
    }

    public double sum(double a, float b, int c) {
        System.out.print("Sum of Intergers with return type double=");
        return (a + b + c);
    }

    public String sum(String msg) {
        return msg;
    }

    public static void main(String args[]) {
        MethodOverloadingDemo mod = new MethodOverloadingDemo();
        mod.sum(1, 2);
        mod.sum(1.0f, 1.0f);
        mod.sum(3, 4);
        System.out.println(mod.sum(1, 2, 3));
        System.out.println(mod.sum(11, 2.0f, 3));
        System.out.println(mod.sum("Return message"));
    }
}
Output:
method overloading
method overloading in java


Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.