Using Method Overloading in java

Method Overloading is a feature that allows a class to have two or more methods having same name, if their argument lists are different. In the last tutorial we discussed constructor overloading that allows a class to have more than one constructors having different argument lists.

Example of Method Overloading 

Main Class:

public class Test {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        OverLoading obj = new OverLoading();
        obj.calcualte(3,4,"Letx code");
    }
  }
Define OverLoading Class:
public class OverLoading {
    public void calcualte(){
        System.out.print("No parameters!!");
    }
    public void calcualte(int a){
        System.out.print("Sum is: "+a+2);
    }
    public void calcualte(int a,String ch){
        System.out.print("Number is:"+a);
        System.out.print("Character is:"+ch);
    }
    public void calcualte(int a,int b,String ch){
        System.out.print("Product is:"+(a*b));
        System.out.print("String is:"+ch);
    }
}

No comments:

Post a Comment