Friday 24 May 2013

METHOD OVERLOADING IN JAVA

Function overloading is a very useful feature provided in the object oriented programming, through which we can have a number of different versions of a function that have same name but have different signature. By different signature, we mean that either the type, count or sequence of the parameter included in the parameter list of the overloaded functions should vary in it's different versions.


Here is a source code illustrating function overloading in Java.
Source code :-
/******************************************************************************/
//fundamental program showing function overloading

class javaapp {

public static void main(String[] args) {

class_b b=new class_b();

double ans1=b.addition(10,20);
double ans2=b.addition(30,40,50);


System.out.println("answer1 = "+ans1+"\nanswer2 = "+ans2);
}

}



class class_b extends class_a{

private double result;
public double addition(int x,int y,int z)
{
/*
function with different arguments
function name is same but argument list or argument type should be diff.
in other way we can say function name can be same but argument signature should be different
*/
result =x+y+z;
return result;
}

}


class class_a {

private double result;
public double addition(int x,int y)
// function with two arguments
{
result =x+y;
return result;

}

}
/******************************************************************************/
Output:-


No comments:

Post a Comment