Demonstrate concept of Polymorphism (Overloading).
Sourse Code:
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
parameter
void test(int a, int b)
{
System.out.println("a & b:"+a
+" "+b);
}
//overload test for 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 version of test()
ob.test();
ob.test(10);
ob.test(10,20);
result=ob.test(123.25);
System.out.println("Result
of ob.test(123.25):"+result);
}
}
Output:
No Parameters
a=10
a & b:10 20
Double a:123.25
Result of ob.test<123.25>:15190.5625