Image

Java - Core Java - Method overloading

Method overloading

Java support to define two or more methods with same names within a class. If we have to perform only one operation, having same name of methods increases the readability of program. All the methods should differ in either by the number or type of parameters. Suppose you have to perform addition of the given numbers but there can be any number of arguments if you write the method such as A (int, int) for two parameters and B(int, int, int) for three parameter then it may be difficult for you as well as other programmer to understand the behavior of the function method because it names differs so we perform method overloading.

Use of java method overloading
  1. Different functionality in both super class and sub class by sharing same signature.
  2. Method overloading is use to provide the specific implementation of a method.
  3. It is used for run time polymorphism.
  4. The behavior can be replaced in the subclass.
  5. Time to invest method signature is r.comced.
  6. The functionality can be enhanced.
WAP for method overloading
class Class1{
    public void multiply(int a,int b){
        System.out.println(a*b);
    }
    public void multiply(int a,int b,int c){
        System.out.println(a*b*c);        
    }
}
public class AJTD6 extends Class1{
    public static void main(String[] args) {
        AJTD6 a=new AJTD6();
        a.multiply(10, 10, 10);
        a.multiply(20, 20);
    }
    
}
O/P
D:\classes>javac AJTD6.java
D:\classes>java AJTD6
1000
400