In Java it is possible to define two or more methods within the same class that share the same name as long as their parameters declarations are different.Then the methods are said to be overloaded and the process is referred to be Method Overloading.
Method Overloading is one of the ways that Java implement polymorphism.When as overloaded method is invoked, java uses the type and number of arguments as its guide to determine which version of the overloaded method to actually call.This overloaded methods must differ in the type and the number of their parameters and it may have different return types.
class OverloadDemo
{
void test( )
{
System.out.println( "No parameters!" );
}
void test(int a , int b)
{
int c;
c=a+b;
System.out.println( " a+b = " + c);
}
double test(double a)
{
return a*a;
}
}
class Overload
{
public static void main( String args[ ] )
{
double result;
OverloadDemo obj = new OverloadDemo( );
obj.test( );
obj.test( 10,20);
result = obj.test(10);
System.out.println( "result="+result);
}
}
OUTPUT
No parameters!
a+b=30
result=100
Method Overloading is one of the ways that Java implement polymorphism.When as overloaded method is invoked, java uses the type and number of arguments as its guide to determine which version of the overloaded method to actually call.This overloaded methods must differ in the type and the number of their parameters and it may have different return types.
class OverloadDemo
{
void test( )
{
System.out.println( "No parameters!" );
}
void test(int a , int b)
{
int c;
c=a+b;
System.out.println( " a+b = " + c);
}
double test(double a)
{
return a*a;
}
}
class Overload
{
public static void main( String args[ ] )
{
double result;
OverloadDemo obj = new OverloadDemo( );
obj.test( );
obj.test( 10,20);
result = obj.test(10);
System.out.println( "result="+result);
}
}
OUTPUT
No parameters!
a+b=30
result=100
No comments:
Post a Comment