Overloading is a process of providing multiple implementation of any Method or constructor inside same class.

Whenever any method is implemented multiple times inside the same class then it is called Method Overloading.

Whenever any constructor is implemented multiple times inside the class then it is called Constructor Overloading.

  • Rules of overloading in Java

    Here is the list of rule which needs to be followed to overload a method or constructor in Java

    • Constructor Signature or Method Signature must be changed for overloading.Signature is made of number of arguments, type of arguments and order of arguments .At least one must be different for overloading . You can change any of these or combinations of them to overload method or constructor in Java.

    • In case of method overloading return type of method is not part of method signature, so just changing the return type will not overload a method in Java. In fact , if the method signature is same and return type is different then we will get compile time error.

  • Overloading Constructor in java

    For overloading constructor in java, define two or more than two constructor in the class but signature of these constructor must be different in order to overload them. Let us see the example of overloading constructor .

    Example)
    drag_handle
     public class Example {
    
    	// define first constructor
    	Example() {
    		System.out.println("Default constructor");
    	}
    
    	// define second constructor , change the argument
    	Example(int a) {
    		System.out.println("One argument constructor " + a);
    	}
    
    	// define third constructor , change the argument
    	Example(int a, int b) {
    		System.out.println("Two argument constructor " + a + " , " + b);
    	}
    
    	// define fourth constructor, change type of argument
    	Example(double b) {
    		System.out.println("one argument(double type) constructor ");
    	}
    
    	/*
    	 * Note: for overloading we can change the - Number of argument, type of
    	 * argument or order of argument but atleast one of these must be different*
    	 */
    
    	// define main method
    	public static void main(String[] args) {
    		new Example();
    		new Example(1);
    		new Example(2, 4);
    		new Example(3.5);
    	}
    }
    
    Output:
    Default constructor
    One argument constructor 1
    Two argument constructor 2 , 4
    one argument(double type) constructor 
  • Overloading Method in java

    For overloading method , define two or more methods of same name in the same class but the argument list must be differ. There 3 different ways to define argument list in order to overload.

    • We can change the number of argument.

    • We can change the data type .

    • We can change the order of argument .

  • Example of Method overloading : changing the number of argument.

    In this example , we will define 3 methods with same name but performing different task . let us see :

    Example)
    drag_handle
     //example of method overloading
    class Example {
    	// sum method for summing two number
    	public void sum(int a, int b) {
    		int c = a + b;
    		System.out.println("sum = " + c);
    	}
    
    	// sum of 3 int type numbers
    	public void sum(int a, int b, int c) {
    		int x = a + b + c;
    		System.out.println("sum = " + x);
    
    	}
    
    	// sum of 4 int type value
    	public void sum(int a, int b, int c, int d) {
    		System.out.println("sum =" + (a + b + c + d));
    	}
    
    	// define main method
    	public static void main(String[] args) {
    		Example ex = new Example();
    		ex.sum(2, 3);
    		ex.sum(1, 2, 33);
    		ex.sum(2, 3, 2, 1);
    
    	}
    }
    
    Output:
    sum = 5
    sum = 36
    sum =8
  • Example of Method overloading : changing the type of arguemet

    In this example , we will define 3 methods with same name but performing different task they will take different type of arguement . let us see :

    Example)
    drag_handle
     //example of method overloading
    class Example {
    	// sum of 2 int type argument
    	public void sum(int a, int b) {
    		System.out.println("sum =" + (a + b));
    	}
    
    	// sum of 2 double type argument
    	public void sum(double a, double b) {
    		System.out.println("sum =" + (a + b));
    	}
    
    	// sum of 2 float type value
    	public void sum(float a, float b) {
    		System.out.println("sum =" + (a + b));
    	}
    	
    	
    	//main method
    	public static void main(String[] args) {
    		Example ex = new Example();
    		ex.sum(2, 3);
    		ex.sum(1.2, 1.6);
    		ex.sum(2f, 3f);// using floating literal
    
    	}
    }
    
    Output:
    sum =5
    sum =2.8
    sum =5.0
  • Example of Method overloading : changing the order of argument.

    In this example , we will keep argument number and type same but change the order of the argument for overloading. let us see the example for better understanding.

    Example)
    drag_handle
     //example of method overloading
    class Example {
    	public void sum(int a, double b) {
    		System.out.println("order is int , double ");
    		System.out.println("Sum = " + (a + b));
    	}
    
    	// change the order of argument list
    	public void sum(double b, int a) {
    		System.out.println("order is double , int ");
    		System.out.println("Sum = " + (a + b));
    	}
    
    	// main method
    	public static void main(String[] args) {
    		Example ex = new Example();
    		ex.sum(2, 3.5);
    		System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@");
    		ex.sum(3.5, 4);
    
    	}
    }
    
    Output:
    order is int , double 
    Sum = 5.5
    @@@@@@@@@@@@@@@@@@@@@@@@@
    order is double , int 
    Sum = 7.5
    
  • What is the advantage Overloading?

    We don't have to create and remember different names for methods which increases the readability of the program.

  • Can we overload methods by changing the return type?

    we can not overload methods by changing the return type. it will generate compile time error if the signature is same and return type is different.

    Example)
    drag_handle
     class Overload {
    	public int test() {
    		return 3;
    	}
    
    	public double test() {
    		return 5.5;
    	}
    }
    Output:
    compileTimeError saying :
    method test() is already defined in class Overload
    public int test()
  • Can we overload main() in Java?

    Yes, we can overload main() in Java by changing the number of argument, type of argument, or order of argument of main method.

    Example)
    drag_handle
     class Overload
    {
    	
    	public static void main(int a) {
    		System.out.println("one argument main method");
    	}
    	public static void main(int a,int b) {
    		System.out.println("Two argument main method");
    	}
    	public static void main(String[] args) {
    		main(2);
    		main(2,2);
    	}
    }
    Output:
    one argument main method
    Two argument main method
  • Can we overload static methods?

    Yes. We can have two ore more static methods with same name, but differences in input parameters.

  • Does Java support Operator Overloading?

    No, Java does not support operator overloading. It is a concept of C++ programming language.

Note

Method overloading is used to implement compile time Polymorphism in java.

What next?

The next topic is Method Overriding in java

Share this page