Write a program to demonstrate method overloading in java.
Program Algorithm:
Step1: Create a class with the main function
Step2: Create one more class with an overloaded function called test.There is a difference in the type of return values and the signature arguments.
Step 3: Create an object of this class inside the main function and call the overloaded method with different arguments.
Program Code:
class Overload
{
public static void main(String args[])
{
OvDe o=new OvDe();
double s;
o.test();
o.test(10);
o.test(32,64);
s=o.test(5.5);
System.out.println("Result of o.test(5.5): "+s);
}
}
class OvDe
{
void test()
{
System.out.println("No parameters");
}
void test(int a)
{
System.out.println("a: "+a);
}
void test(int a,int b)
{
System.out.println("a and b: "+a+" "+b);
}
double test(double a)
{
System.out.println("double a: "+a);
return a*a;
}
}
Sample Inputs and Outputs:
No parameters
a: 10
a and b: 32 64
double a: 5.5
Result of o.test(5.5): 30.25
Post A Comment:
0 comments: