Method overloading

In java it is possible to define two or more methods within the same class that can share the same name but different parameter declaration .in this case we can say method is overloaded, and the process is called as method overloading.
Method over loading is one of the ways that implements polymorphism. 

EXAMPLE:

class Hello
{
int a;
int x;
void test()
{
System.out.println("i am default parameter");
}
void test(int a)
{
System.out.println("i am one parameter");
System.out.println(a);
}
void test(int a,int b)
{
System.out.println("i am two parameter");
System.out.println(a);
System.out.println(b);
}
}
}


double test(double x)
{
System.out.println("i am one double  parameter");
System.out.println(x);
return x*x;

class T9
{
public static void main(String as[])
{
double result;
Hello h=new Hello();
h.test();
h.test(99);
h.test(50,40);
h.test(32.56);
result=h.test(123.25);
System.out.println("double return value:-----"+result);
}

Output of program   :-------

i am default parameter
i am one parameter
99
i am two parameter
50
40
i am one double  parameter
32.56
i am one double  parameter
123.25
double return value:-----15190.5625


test () is overloaded four times. the first version takes no parameter the second takes one integer parameter and the third takes two integer parameters and the fourth takes one double parameter . Return types don’t play a role in overloaded resolution.



class Hello
{
int a;
int x;
void test()
{
System.out.println("i am default parameter");
}
void test(int a,int b)
{
System.out.println("i am two parameter");
System.out.println(a);
System.out.println(b);
}
double test(double x)
{
System.out.println("i am one double  parameter");
System.out.println(x);
return x*x;
}
}


class T8
{
public static void main(String   as[])
{
int i=66;
double result;
Hello h=new Hello();
h.test();
h.test(99);
h.test(i);
h.test(50,40);
h.test(32.56);
result=h.test(123.25);
System.out.println
("doublereturnvalue:-----"+result);
 }
}

OUTPUT:
i am default parameter
i am one double  parameter
99.0
i am one double  parameter
66.0
i am two parameter
50
40
i am one double  parameter
32.56
i am one double  parameter
123.25
double return value:-----15190.5625
   

0 comments :

Post a Comment