- method with same name but different parameter list or return type are called overloaded method
- the overloaded method can be in same class or extended class.It is one of the way of polymorphism in java
PROGRAM:
````````````````
class a
{
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 ab
{
public static void main(String args[])
{
a ob = new a();
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);
}
}
OUTPUT:
~~~~~~~
No parameters
a: 10
a and b: 10 20
double a: 123.2
Result of ob.test(123.2): 15178.240000000002
No comments:
Post a Comment