JAVA Method Overloading

  • When a particular method is defined multiple times in a class to perform multiple tasks, is known as method overloading.
  • Here method name is same but they are different by their signatures, that means method will take different types of arguments. Otherwise compile-time error will arise.
  • Method overloading is an example of compile-time polymorphism.
  • When an overloaded method is invoked, JVM invokes the method according to the type and/or number of arguments that the method is taking.

OverloadingExample1.java

class Test {
    void show() {
        System.out.println("JAVA means SILAN Technology");
    }
    void show(int x) {
        System.out.println("x=" + x);
    }
    void show(double p, double q) {
        System.out.println(p + " " + q);
    }

    class OverloadingExample1 {
        public static void main(String[] args) {
            Test t = new Test();
            t.show();
            t.show(100);
            t.show(5.2, 4.6);
        }
    }

OUTPUT
 <

Another Example;

 

OverloadingExample2.java

class AreaExamples {
    void area(int length, int breadth) {
        int a;
        a = length * breadth;
        System.out.println("area of rectangle is" + a);
    }
    void area(double base, double height) {
        double ar;
        ar = 0.5 * base * height;
        System.out.println("area of triangle is" + ar);
    }
    void area(double r) {
        double arr;
        arr = 3.141 * r * r;
        System.out.println("area of circle is" + arr);
    }


class OverloadingExample2

  {
     public static void main(String[] args)
       {
         AreaExamples ob=new AreaExamples();
         ob.area(5,6);
         ob.area(2.5,3.4);
         ob.area(4.6);
       }
  }

Output

Regards:

Trilochan Tarai
SILAN Technology, BBSR
+91-9439202111

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology, BBSR
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment