Programmer Coding

Way(method) Overloading in java

If a class has multiple way(method) s having same name but different in parameters, it is
knownas Way(method) Overloading.
If we have to apply only one operation, having same name of the way(method) s increases the
read ability of the program.

Way(method) Overloading: changing no. of arguments

In this example, we have produced two way(method) s, first add() way(method) applys
addition of two numbers and second add way(method) applys addition of three numbers.
In this example, we are creating static way(method) s so that we don’t need to produce instance for
callingway(method) s.
class Adder{
static int add(int a,int b){return a+b;}
static int add(int a,int b,int c){return a+b+c;}
}
class TestOverloading1{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}}
Output:

22
33

Way(method) Overloading: changing data type of arguments

In this example, we have produced two way(method) s that differs in data type. The first
add way(method) receives two integer arguments and second add way(method) receives
two double arguments.

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top