Method Overloading in JAVA || What is method overloading in java?

Method Overloading:

Method overloading is one of the ways that Java implements isomorphism. Java uses the type and/or number of arguments as its guide to determine which version of the overloaded method to actually call. Thus, overloaded methods must differ in the type and/or number of their parameters.

Example:

class methodOverload {

void Sum() //No Parameter is defined

{ System.out.println("No Argument is given."); }

void Sum(int a) //One int Parameter is defined

{ System.out.println("Only One argument is given."); }

void Sum(int a, int b) //Two int Parameters for sum

{ System.out.println("Sum of two given integers : " + (a+b)); }

void Sum(int a, float b) //One int and one float parameter

{ System.out.println("Sum of int + float :" + (a+b)); }

void Sum(double a,double b) //Two double Parameters

{ System.out.println("Sum of two doubles :" + (a+b)); }

public static void main(String args[]) //Main

{

methodOverload test = new methodOverload();

test.Sum();

test.Sum(5);

test.Sum(5,5);

test.Sum(5,3.5f);

test.Sum(3.5,2.95);

}

}

Post a Comment

Please do not enter any spam link in the comment box