Method Overloading in Java

Filed Under: Java

The concept of Method Overloading in Java is where a class can have multiple methods with the same name, provided that their argument constructions are different. Method Overloading is comparable to constructor overloading where we can implement multiple constructors (also the same name), provided that these constructors have different argument construction.

Method Overloading in Java

  • Method name should be exactly same. Java is case sensitive, so two methods with name foo()
    and fOO() are totally different and doesn’t come under method overloading in java.
  • The difference between overloaded methods are the arguments. Overloaded methods should have different method arguments. For example, different number of parameters, different data types of parameters etc.
  • Method return type have no significance for overloading, java compiler will complain about duplicate method in the class.
  • Method parameter names have no significance in method overloading. So in terms of java, foo(int index) and foo(int i) are same.
  • Method overloading is also called compile time polymorphism. Read more at OOPS Concepts in Java.

Let’s look into some examples of method overloading in java.

Method Overloading in Java Example


package com.journaldev.methodoverloading;

public class GetAverage {
	private static void gatherInput(int dataOne, int dataTwo) {
	};

	private static void gatherInput(int dataThree, String dataFour, float dataFive) {
	};
}

The snippet above shows a classic example of Method Overloading. Two gatherInput() methods are declared under GetAverage Class. In usual cases, java compiler would display the error: Duplicate method gatherInput() in type GetAverage. However, in this case, we see that the two methods possess different argument list, which makes this approach in java permissible.

Instances where Method Overloading is permissible

  1. Parameter Quantity: Method Overloading is allowed within the class given that the number of parameters of the overloaded methods are not the same.
    
    package com.journaldev.methodoverloading;
    
    public class GetAverage {
    	private static void gatherInput(int dataOne, int dataTwo) {
    	};
    
    	private static void gatherInput(int dataOne, int dataTwo, int dataThree) {
    	};
    }
    
  2. Parameter Data Types: Method Overloading is allowed within the class given that at least one pair of parameters, from each overloaded method, are of different data type.
    
    public class GetAverage {
    	private static void gatherInput(int dataOne, int dataTwo) {
    	};
    
    	private static void gatherInput(int dataOne, double dataTwo) {
    	};
    }
    
  3. Order of Parameters: Method Overloading is allowed within the class given that the order of the data type variables are not the same for overloaded methods.
    
    public class GetAverage {
    	private static void gatherInput(int dataOne, double dataTwo) {
    	};
    
    	private static void gatherInput(double dataOne, int dataTwo) {
    	};
    }
    

Instance where Method Overloading is Impermissible

  • Method Return Type: Method Overloading is prohibited within the class with methods having identical parameter list but with different method return type. Bear in mind that Overloading focuses on the parameters, not the return type.
    
    public class GetAverage {
    	private static int gatherInput(int dataOne, double dataTwo) {
    		return 0;
    	};
    
    	private static double gatherInput(int dataOne, double dataTwo) {
    		return 0;
    	};
    }
    

    Above code will produce compiler error: Duplicate method gatherInput(int, double) in type GetAverage.

method overloading in java, overloading in java

Type Promotion in Java

Type Promotion is a java concept where a data type of smaller rank is promoted to the data type of higher rank.
The figure below illustrates the data type size promotion order:

type promotion in java, java overloading

As can be observed from the above diagram, byte may be promoted to short. Int data type have three possible promotions: double, long, or float.

Type Promotion and Method Overloading in Java

Type Promotion goes hand in hand with Method Overloading in Java. Type Promotion extends the logic of method overloading when it comes to deciding which overloaded method is used, in cases where values passed does not coincide with any of the parameter data types established.

Note: Passed value data type is automatically promoted to its data type next in line (refer to figure above), if no matching data type is found.

Let’s look into some of the examples of type promotion and overloading in java.

  1. Type Promotion Example 1:
    
    package com.journaldev.methodoverloading;
    
    public class TypePromotionExample {
    
    	void add(float dataOne, float dataTwo) {
    		System.out.println(dataOne + dataTwo + " type float");
    	}
    
    	void add(long dataOne, long dataTwo) {
    		System.out.println(dataOne + dataTwo + " type long");
    	}
    
    	public static void main(String[] args) {
    		TypePromotionExample tpe = new TypePromotionExample();
    		tpe.add(5, 5); // integer values are passed
    	}
    
    }
    

    Output: 10 type long

    Two integers are passed in overloaded add() method. Since no overload methods possess (int, int) arguments, the passed values will be promoted to the nearest bigger type i.e. long.

  2. Type Promotion Example 2:
    
    package com.journaldev.methodoverloading;
    
    public class TypePromotionExample {
    
    	void add(float dataOne, float dataTwo){
    		System.out.println(dataOne+dataTwo + " type float");
    	}
    	
    	void add(double dataOne, double dataTwo){
    		System.out.println(dataOne+dataTwo + " type long");
    	}
    
    	public static void main(String[] args) {
    		TypePromotionExample tpe = new TypePromotionExample();
    		tpe.add(5, 5); // integer values are passed
    	}
    
    }
    

    Output: 10.0 type float

    Here the nearest bigger type for int is float, hence the output.

  3. Type Promotion Example 3:
    
    package com.journaldev.methodoverloading;
    
    public class TypePromotionExample {
    
    	void add(double dataOne, int dataTwo){
    		System.out.println("1st method invoked.");
    	}
    	
    	void add(long dataOne, long dataTwo){
    		System.out.println("2nd method invoked.");
    	}
    
    	public static void main(String[] args) {
    		TypePromotionExample tpe = new TypePromotionExample();
    		tpe.add(5, 5); // integer values are passed
    	}
    
    }
    

    Here java compiler will produce error as The method add(double, int) is ambiguous for the type TypePromotionExample. It’s because both methods are applicable and type conversion is required for any one of them to execute, so java compiler can’t decide which one to go for.

  4. overloading in java method ambiguous error

Method Overloading in Java Valid/Invalid Cases

Let’s look at some code snippets for method overloading use case, whether they are valid or not.

  • Method Overloading in Java Example 1
    
    void method(int dataOne, int dataTwo, int dataThree){}
    
    void method(int dataFour, int dataFive, int dataSix){}
    

    Compile time error message Duplicate method method(int, int, int). Even if the variable names differ, java only looks into data types being created and in this example, are both identical(int, int, int).

  • Method Overloading in Java Example 2
    
    void method(int dataOne, int dataTwo){}
    
    void method(float dataThree, float dataFour){}
    

    Valid case of method overloading. Data types of arguments are different.

  • Method Overloading in Java Example 3
    
    void method(int dataOne, int dataTwo){}
    
    void method(int dataThree){}
    

    Valid case of method overloading, number of arguments are different.

  • Method Overloading in Java Example 4
    
    void method(int dataOne, float dataTwo){}
    
    void method(float dataThree, int dataFour){}
    

    Valid case of method overloading. Sequence of the data types of parameters are different, first method having (int, float) and second having (float, int).

  • Method Overloading in Java Example 5
    
    int method(int dataOne, float dataTwo){ return 0;}
    	
    float method(int dataThree, float dataFour){ return 0.0;}
    

    Compile time error because argument lists are identical. Even if return type of methods and variable names are different, it’s not a valid case. Since return type or variable name literals are not criteria for method overloading.

That’s all for method overloading in java.

Reference: Wikipedia

Comments

  1. Satish Kumar says:

    I understood better after read your content. before I searched for a different website but there was not enough. Thanks, Sir.
    But I follow mostly JavaTpoint. Is it Good for me?

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages