Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Method Overloading Program in Java

Last Updated on December 21, 2022 by Aveek

In this article, we will discuss method overloading in Java and we will write a method overloading program in Java based on different criteria of method overloading. So, let’s get started by learning the meaning of method overloading.

What is Method Overloading?

Method overloading is also known as compile-time polymorphism or early binding in Java. Method overloading is the concept of having different methods with the same name in the same class but having a different signature and the compiler decides while compiling the program which method out of the available methods will be called. Thus it is known as compile-time polymorphism.

A method overloading program in Java is shown below.

Method Overloading Program in Java – Sum of Numbers

public class Main {
    public static int sum(int a, int b) {
        return a+b;
    }
    public static int sum(int a, int b, int c) {
        return a+b+c;
    }
    public static void main(String[] args) {
        System.out.println(sum(2,3));
        System.out.println(sum(2,3,5));
    }
}

As shown in the code above, there are 2 methods named sum() in the Main class and one accepts 2 parameters while the other accepts 3 parameters. When we pass 2 parameters, the compiler decides during the compile time to call the method with 2 parameters and when we pass 3 parameters, the compiler decides to call the method with 3 arguments during the compile time only.

The concept of method overloading follows various ways. There are following three ways in which method overloading occurs:

  1. If the number of parameters of the methods differs from each other.
  2. If the data type of the parameters of the methods differs from each other.
  3. If the order of the parameters of the methods differs from each other.

Let us see the method overloading program in Java in each of the following cases and understand them in detail.

Method Overloading Program in Java – Different Number of Parameters

Method Overloading happens in Java when the number of parameters of the methods is different. For instance, in the above example too, there were 2 and 3 parameters respectively in the sum() methods. Similarly, here we can write a method overloading program in Java with different parameters for the multiplication operation. The program is given below.

Method Overloading Program in Java – Different Number of Parameters (Multiplication of Numbers)

public class Main {
    public static int multiply(int a, int b) {
        return a*b;
    }
    public static int multiply(int a, int b, int c) {
        return a*b*c;
    }
    public static void main(String[] args) {
        System.out.println(multiply(10,20));
        System.out.println(multiply(10,20,5));
    }
}

So, this was method overloading due to a different number of parameters. Let us now move on to the next way of method overloading in Java.

Method Overloading Program in Java – Different Data types of Parameters

The method overloading happens in Java due to different data types of parameters also. Consider a Student class. A student has a roll number and a name. The show function is used to display these details as shown below.

Method Overloading Program in Java – Different Data type of Parameters

class Student {
    String name;
    int rollNo;
    Student() {
    }
    Student(String name, int rollNo) {
        this.name = name;
        this.rollNo = rollNo;
    }
    public void show(String name) {
        System.out.println("You have entered the name of the student as " + name);
        System.out.println("The roll number of " + name + " is = " + rollNo);
    }
    public void show(int rollNo) {
        System.out.println("You have entered the rollNo of the student = " + rollNo);
        System.out.println("The name of the student with the roll number = " + rollNo + " is " + name);
    }
}
public class Main {
    public static void main(String[] args) {
        Student s1 = new Student("Guneet",14);
        Student s2 = new Student("Yash",10);
        s1.show(s1.name); 
        s2.show(s2.rollNo);
    }
}

So, as shown in the program above, if the data type of the parameter is different, the compiler is able to decide which function to call and thus method overloading is taking place. In the above code, there was only one parameter and the 2 functions were having String and int as the 2 different data types.

Let us now move to the third way of method overloading i.e. changing the order of parameters.

Method Overloading Program in Java – Different Order of Parameters

Consider the above Student class only. Now, it might happen that someone else who has not created this class and wants to create an object of the Student class in some other file might pass the roll number and then the name in the constructor rather than passing the first name and then the roll number. So, we should provide such a facility. This is done as shown in the program below.

Method Overloading Program in Java – Different Order of Parameters

class Student {
    String name;
    int rollNo;
    Student() {
    }
    // int,string -> parameter order
    Student(int rollNo, String name) {
        this.name = name;
        this.rollNo = rollNo;
    }
    // string,int -> parameter order
    Student(String name, int rollNo) {
        this.name = name;
        this.rollNo = rollNo;
    }
    public void show() {
        System.out.println("Name = " + name + " Roll No = " + rollNo);
    }
}
public class Main {
    public static void main(String[] args) {
        Student s1 = new Student("Guneet",14);
        Student s2 = new Student(10,"Yash");
        s1.show(); 
        s2.show();
    }
}

So, in the above program, we are passing a string and then an integer to one constructor and an integer and then a string to another constructor. Well, this can be done with normal methods also.

So, these are the 3 ways of implementing method overloading in Java.
Let us now see some fun questions and answer them.

Can we overload methods if they only differ in return type?

The following instance shows 2 methods that differ in return type only.

public static void greet(String name) {
System.out.println(“Hello” + name);
}
public static String greet(String name) {
return (“Hello” + name);
}

So, in the above instance, one function just prints the greeting while the other returns it as a String. They have the same name and the same number and the data type of the parameter. They can’t be included in any of the 3 categories discussed above, hence this is not method overloading. So, this will give an error in Java.

Now, let’s answer the next question.

Can we overload static methods in Java?

All the examples shown above were of static methods being overloaded. So, yes, static methods can be overloaded in Java.

Can 2 methods that differ only by static keyword be overloaded in Java?

Consider the program shown below.

Methods that differ only by static keyword

class Student {
    String name;
    int rollNo;
    Student() {
    }
    Student(int rollNo, String name) {
        this.name = name;
        this.rollNo = rollNo;
    }
    Student(String name, int rollNo) {
        this.name = name;
        this.rollNo = rollNo;
    }
    public static void show() {
        System.out.println("This show method shoes we are in Class Student");
    }
    public void show() {
        System.out.println("Name = " + name + " Roll No = " + rollNo);
    }
}
public class Main {
    public static void main(String[] args) {
        Student s1 = new Student("Guneet",14);
        Student s2 = new Student(10,"Yash");
        s1.show(); 
        s2.show();
    }
}

So, there are two show() methods that differ only by static keyword. This is not method overloading and will give an error because it can’t be described as any of the 3 ways of method overloading discussed above.

Now, let us answer the last and very interesting question.

Can we overload the “main” method in Java?

The main() method is also just a method. So, yes it is possible to overload the main method in Java as shown below.

Method Overloading in Java – Overloading the main() Method

public class Main {
    public static void main(String[] args) {
        System.out.println("Welcome to PrepBytes (From Main method)");
        main("Guneet");
    }
    public static void main(String arg) {
        System.out.println("Welcome to PrepBytes " + arg);
        main("Guneet","Yash");
    }
    public static void main(String arg1,String arg2) {
        System.out.println("Welcome to PrepBytes " + arg1 + " and " + arg2);
    }
}

In the code above, we have the normal main method that has a String array as a parameter to accept arguments. From this main() method, we have called another main() method that only has one string as a parameter, and from that main() method, we have called the last main method that has 2 strings as parameters. So, the main() method can be overloaded in Java.

So, with this, we come to an end of our discussion on method overloading in Java. We hope that you have understood all three methods of overloading i.e. method overloading due to the different number of parameters, method overloading due to the different types of parameters, and method overloading due to the different order of parameters. We hope that you have enjoyed learning the answers to the fun questions related to method overloading as well.

So, with this, we come to conclude this article. We hope to see you again soon at PrepBytes.

Leave a Reply

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