07-Method Overloading

  • Method Overloading means declare multiple methods in the same class with the same method name but with different method parameters.

class Test{

    void m(int x, int y){
        System.out.println("m method with integer values");
    }
    void m(double x,double y)
        System.out.println("m method with double values");
    }

    public static void main(String args[]){
        Test t = new Test();
        t.m(2.4,5.8);
        t.m(2,6);
        t.m(4,6.7);// In here its treat as a double

    }
}

Now lets try with a different example

public class Testing {

    void m(int x, double y){
        System.out.println("m method with integer values");
    }
    void m(double x,int y){
        System.out.println("m method with double values");
    }

    public static void main(String args[]){
         Testing t = new Testing();
         t.m(2.4,5.8); // compile error
         t.m(2,6);//compile error
         t.m(4,6.7);// In here 4 its treat as a double. no compile error
    }
}

Leave a comment