Methods in Java are a complete sequence of actions (instructions) aimed at solving a particular problem. In fact, these are functions (aka procedures, subroutines) of earlier, non-OOP languages. Only these functions are members of classes and, to distinguish them from ordinary functions, according to the terminology of object-oriented programming, are called methods. Methods are always defined inside classes.
Overloading means making the same method have different behaviour depending on its arguments. We can create another add method with three int arguments which will return sum of first two arguments minus third argument.
int add(int a, int b, int c)
{
return ((a+b)-c);
}
Now when we call add method, its behaviour is dictated by the parameters passed during its call.
...
x = add(10,20); // x = 30
y = add(10,20,30); // y = 0
Comments
Leave a comment