Data Types and Operators
(Non-Primitive Data Types)
Control Flow Statements
Conditional Statements
Looping Statements
Branching Statements
Object-Oriented Programming (OOP)
Exception Handling
Collections Framework
Overview of Collections
Java I/O
Multithreading
GUI Programming with Swing
Advanced Topics
JAVA CODE
Java Basics
Working with Objects
Arrays, Conditionals, and Loops
Creating Classes and Applications in Java
More About Methods
Java Applet Basics
Graphics, Fonts, and Color
Simple Animation and Threads
More Animation, Images, and Sound
Managing Simple Events and Interactivity
Creating User Interfaces with the awt
Windows, Networking, and Other Tidbits
Modifiers, Access Control, and Class Design
Packages and Interfaces
Exceptions
Multithreading
Streams and I/O
Using Native Methods and Libraries
Under the Hood
Java Programming Tools
Working with Data Structures in Java
Advanced Animation and Media
Fun with Image Filters
Client/Server Networking in Java
Emerging Technologies
appendix A :- Language Summary
appendix B :- Class Hierarchy Diagrams
appendix C The Java Class Library
appendix D Bytecodes Reference
appendix E java.applet Package Reference
appendix F java.awt Package Reference
appendix G java.awt.image Package Reference
appendix H java.awt.peer Package Reference
appendix I java.io Package Reference
appendix J java.lang Package Reference
appendix K java.net Package Reference
appendix L java.util Package Reference

Method overloading in Java is a feature that allows a class to have more than one method with the same name, provided their parameter lists are different. It is a way to achieve polymorphism at compile time. Method overloading can be achieved by:

  1. Changing the number of parameters in a method.
  2. Changing the data type of parameters.
  3. Changing the sequence of parameters if they are of different types.

Key Points:

  • Overloaded methods must differ in the number or type of their parameters.
  • Method return type can be different, but it alone is not sufficient to overload a method.
  • It enhances the readability of the code.

Example of Method Overloading

Let’s look at a simple example demonstrating method overloading:

				
					public class MethodOverloadingExample {

    // Method with no parameters
    public void display() {
        System.out.println("Display method with no parameters.");
    }

    // Method with one integer parameter
    public void display(int a) {
        System.out.println("Display method with one integer parameter: " + a);
    }

    // Method with one string parameter
    public void display(String a) {
        System.out.println("Display method with one string parameter: " + a);
    }

    // Method with two parameters (integer and string)
    public void display(int a, String b) {
        System.out.println("Display method with two parameters (int, String): " + a + ", " + b);
    }

    // Method with two parameters (string and integer)
    public void display(String a, int b) {
        System.out.println("Display method with two parameters (String, int): " + a + ", " + b);
    }

    public static void main(String[] args) {
        MethodOverloadingExample example = new MethodOverloadingExample();

        // Calling the methods with different parameters
        example.display();
        example.display(10);
        example.display("Hello");
        example.display(20, "World");
        example.display("Java", 30);
    }
}

				
			

Output:

				
					Display method with no parameters.
Display method with one integer parameter: 10
Display method with one string parameter: Hello
Display method with two parameters (int, String): 20, World
Display method with two parameters (String, int): Java, 30

				
			

Explanation:

  • display()‘ – This method is called without any arguments and the corresponding method with no parameters is executed.
  • display(int a)‘ – This method is called with one integer argument.
  • display(String a)‘ – This method is called with

Deep Dive into Method Overloading

1. Overloading by Changing the Number of Parameters:

This is the most straightforward way to overload a method. You simply provide different numbers of parameters.

				
					public class OverloadingByNumberOfParameters {

    public void print() {
        System.out.println("No parameters");
    }

    public void print(int a) {
        System.out.println("One parameter: " + a);
    }

    public void print(int a, int b) {
        System.out.println("Two parameters: " + a + ", " + b);
    }

    public static void main(String[] args) {
        OverloadingByNumberOfParameters obj = new OverloadingByNumberOfParameters();
        obj.print();
        obj.print(10);
        obj.print(10, 20);
    }
}

				
			

Output:

				
					No parameters
One parameter: 10
Two parameters: 10, 20

				
			

2. Overloading by Changing the Type of Parameters:

You can overload methods by changing the data types of the parameters.

				
					public class OverloadingByTypeOfParameters {

    public void display(int a) {
        System.out.println("Parameter of type int: " + a);
    }

    public void display(double a) {
        System.out.println("Parameter of type double: " + a);
    }

    public void display(String a) {
        System.out.println("Parameter of type String: " + a);
    }

    public static void main(String[] args) {
        OverloadingByTypeOfParameters obj = new OverloadingByTypeOfParameters();
        obj.display(10);
        obj.display(10.5);
        obj.display("Hello");
    }
}

				
			

Output:

				
					Parameter of type int: 10
Parameter of type double: 10.5
Parameter of type String: Hello

				
			

3. Overloading by Changing the Sequence of Parameters:

This involves using the same types but in different sequences.

				
					public class OverloadingBySequenceOfParameters {

    public void display(int a, String b) {
        System.out.println("Parameters: int, String - " + a + ", " + b);
    }

    public void display(String a, int b) {
        System.out.println("Parameters: String, int - " + a + ", " + b);
    }

    public static void main(String[] args) {
        OverloadingBySequenceOfParameters obj = new OverloadingBySequenceOfParameters();
        obj.display(10, "Hello");
        obj.display("Hello", 10);
    }
}

				
			

Output:

				
					Parameters: int, String - 10, Hello
Parameters: String, int - Hello, 10

				
			

4. Compile-Time Polymorphism:

Method overloading is a type of compile-time polymorphism. The compiler determines the method to invoke at compile time based on the method signature. Here’s an example demonstrating this concept:

				
					public class CompileTimePolymorphism {

    public void show() {
        System.out.println("No arguments");
    }

    public void show(int a) {
        System.out.println("Integer argument: " + a);
    }

    public void show(String a) {
        System.out.println("String argument: " + a);
    }

    public static void main(String[] args) {
        CompileTimePolymorphism obj = new CompileTimePolymorphism();
        obj.show();
        obj.show(5);
        obj.show("Hello");
    }
}

				
			

Output:

				
					No arguments
Integer argument: 5
String argument: Hello

				
			

In the above example, based on the method call during runtime, the corresponding overloaded method is executed.

Important Considerations:

  1. Return Type: While return type alone cannot differentiate overloaded methods, it is part of the method signature. Overloaded methods can have different return types, but the parameter lists must differ.

  2. Access Modifiers: Methods can have different access modifiers (public, private, etc.), but this doesn’t affect overloading.

  3. Exceptions: Overloaded methods can throw different exceptions.

  4. Static Methods: Static methods can also be overloaded, but overloading static and non-static methods with the same signature is not allowed.

  5. Varargs: Varargs (variable-length arguments) can also be used in method overloading. However, care must be taken to avoid ambiguity.

				
					public class OverloadingWithVarargs {

    public void display(int... numbers) {
        System.out.println("Varargs: " + Arrays.toString(numbers));
    }

    public void display(String message, int... numbers) {
        System.out.println("Message: " + message + ", Varargs: " + Arrays.toString(numbers));
    }

    public static void main(String[] args) {
        OverloadingWithVarargs obj = new OverloadingWithVarargs();
        obj.display(1, 2, 3);
        obj.display("Hello", 4, 5, 6);
    }
}

				
			

Output:

				
					Varargs: [1, 2, 3]
Message: Hello, Varargs: [4, 5, 6]

				
			

By understanding these nuances and variations, you can effectively utilize method overloading to create flexible and readable code in Java.

Scroll to Top