Get Started: Method Overloading in Java

In This Article, You Will Learn about Method Overloading.

Method Overloading – Before moving ahead, let’s know a bit about Java Modifiers.

Table of Contents

Method

The method is a collection of instructions or blocks of code that are written to do a specific task.

Method Signature

It is a part of the method declaration. It includes –

  • Name of the method
  • Name of the parameter
  • Datatype of the parameter
  • Order of the parameters

Example: Briefly explanation of method.

				
					void add(int x, float y){
    // body of the method
}

int add(int x, float y, int z){
    // body of the method
}
				
			

Explanation – In both methods above, add() is a method name, whereas int and float are the datatypes of the parameters, i.e., x and y.

The only difference in both methods is that void is written in the first method, which means it returns nothing, whereas, in the second method, int is written, which means the output will be int datatype.

Method Body

It is a part of the method declaration which contains all the tasks to be performed. It is enclosed within the pair of curly braces {}.

Return Type

Every method returns some value which is known as the Return type. It may return a primitive data type, object, etc. If the method does not return anything, we use the void keyword.

Naming a Method

The method name must start with a lowercase letter. In the multi-word method name, the first letter of each word must be in uppercase except the first word.

Example – Lowercase method’s name – 

add(), subtract(), subject(), Code()

Example – Multi-word method’s name – 

addNumber(), subtractScore(), subjectMarks(), pinCode()

Types of Method

There are two types of methods in Java. Predefined Method and User-defined Method.

Pre-defined

Pre-defined is a built-in/already defined method. Ex: equals(), length(), sqrt(), etc. Java doesn’t allow any modification in it.

E.g., Find the square-root of 25.

				
					public class Main{
    public static void main(String[] args){
        System.out.println(Math.sqrt(25));
    }
}
				
			

In this example, three built-in methods namely main(), print(), and sqrt() have been used. You can see these methods have been used directly without declaration because they are predefined. The sqrt() method is a method of the Math class that returns the square root of a number.

User-defined

User-defined is type of method that is written by user, it can be modified as per need. Ex: myName(), totalNum(), etc.

				
					public class Main{
    public static int sumNumbers(int x, int y, int z){
        return x + y + z;
    }
}
				
			

In this example, a user-defined method is defined named sumNumbers() that contains three parameters: x, y, and z and whose datatypes are int.

Method overloading – When a same method is defined with different parameters in a class.

Note: Overloaded method must have different parameters.

Suppose you have to perform an addition of the given numbers. If you write the method, there can be any number of arguments, such as a(int, int) for two parameters and b(int, int, int) for three parameters. It may be difficult for you and other programmers to understand the method’s behaviour because its name differs.

Two ways to overload the method

  • By changing number of arguments
  • By changing the data type

By changing number of arguments 

				
					public class differentFunction{
    public void addNumbers(int x, int y) {
        System.out.println("The sum of " + x + " + " + y + " = " + (x+y));
    }
    public void addNumbers(int x, int y, int z) {
        System.out.println("The sum of " + x + " + " + y + " + " + z + " = " + (x+y+z));
    }
    public static void main(String[] args) {
        // created object to call method
        differentFunction add = new differentFunction();
        add.addNumbers(10, 20);
        add.addNumbers(10, 20, 30);   
    }
}
				
			

Explanation – In both methods above, addNumbers() is a method name, whereas int is the datatype of the both method’s parameters, i.e., x and y and x, y, and z contained in a class named differentFunction.

Later, created an object named add to call methods and assigned values to parameters.

By changing the data type

				
					public class differentFunction{
    public void addNumbers(int x, int y) {
        System.out.println("The sum of " + x + " + " + y + " = " + (x+y));
    }
    public void addNumbers(int x, float y) {
        System.out.println("The sum of " + x + " + " + y + " = " + (x+y));
    }
    public static void main(String[] args) {
        // created object to call method
        differentFunction add = new differentFunction();
        add.addNumbers(10, 20);
        add.addNumbers(10, 20.0f);   
    }
}
				
			

Explanation – Everything is the same as in the above example except for one thing: notice the second parameter (i.e., y) type (i.e., float) of the second method; variable y is a float type. While assigning value, f represents that value is floating type (means, decimal number).

If you find anything incorrect in the above-discussed topic and have further questions, please comment below.

Connect on:

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on