0% found this document useful (0 votes)
37 views

Method Overloading Program

This document demonstrates method overloading in Java by defining multiple versions of a test method that vary by parameters. It defines a test method with no parameters, one integer parameter, two integer parameters, and one double parameter. The main method then calls each version of test to display the overloading in action.

Uploaded by

leela
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Method Overloading Program

This document demonstrates method overloading in Java by defining multiple versions of a test method that vary by parameters. It defines a test method with no parameters, one integer parameter, two integer parameters, and one double parameter. The main method then calls each version of test to display the overloading in action.

Uploaded by

leela
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

// Demonstrate method overloading.

class OverloadDemo
{
void test()
{
System.out.println("No parameters");
}

// Overload test for one integer parameter.


void test(int a)
{
System.out.println("a: " + a);
}

// Overload test for two integer parameters.


void test(int a, int b)
{
System.out.println("a and b: " + a + " " + b);
}

// overload test for a double parameter


double test(double a)
{
System.out.println("double a: " + a);
return a*a;
}
}

class Overload
{
public static void main(String args[])
{
OverloadDemo ob = new OverloadDemo();
double result;

// call all versions of test()


ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.2);
System.out.println("Result of ob.test(123.2): " + result);
}
}

Generates the following output using method overloading:

No parameters
a: 10
a and b: 10 20
double a: 123.2
Result of ob.test(123.2): 15178.24

You might also like