method overloading in java

Method overloading:

                In a class, many methods have the same name but different parameters, it is known as method overloading. In a class, two methods have the same name but a different signature that indicates method is overloaded.
The method overloading increases the readability of the program and we can overload the main () in java. Method overloading is not possible by changing the return type of the method. Method overloading is possible within the class only.There are two ways to overload the method
·         By changing parameters
·         By changing the data type.

Syntax:
                public <data type> <method name>(parameter1,parameter2,…..)
                public <data type> <method name>(parameter1 , parameter2,…)

Example:
                public void difference(int i, int j)
                public void difference (int i, int j, int k)
                public void difference (double i, double j)

Sample java program:
                class Sample{
public void difference(int i, int j){
       System.out.println(i-j); }
public void difference (int i, int j, int k) {
        System.out.println(i-j-k); }
public void difference (double i, double j) {
        System.out.println(i-j); }
public static void main(String args[]){
Sample s= new Sample();
s. difference(10,8);
s. difference(15,3,2);
s. difference(10.0,5.0);
}
}


Interview Questions:               
Define method overloading?
What is the use of method overloading?
What is the difference between method overloading and method overriding?
Can we overload main () method?
How many we can overload a method?               
 Differences between method overriding and method overloading


EmoticonEmoticon