Understanding OOP Concepts: Method Overloading

Published on February 23, 2020 35 sec read

Method Overloading is a technique that allows a class multiple methods having the same name but different in parameters. It’s like constructor overloading. This technique is also called compile-time polymorphism or static binding or early binding. 

There are different ways to overload a method in Java. They are:

  1. Number of parameters
  2. Data type of parameters
  3. Sequence of Data type of parameters

Number of parameters

This method overloading is done by having different number of parameters. The example:

class MyClass {
    public void add(int i) {
        // code
    }

    public void add(int i, int j) {
        // code
    }
}

Data type of parameters

This method overloading is done by having different data types of parameters. The example:

class MyClass {
    public void add(int i, int j) {
        // code
    }

    public void add(int i, float j) {
        // code
    }
}

Sequence of Data type of parameters

This method overloading is done by having sequence of data type of parameters. The example:

class MyClass {
    public void add(int i, float j) {
        // code
    }

    public void add(float i, int j) {
        // code
    }
}

Monthly Newsletter

One email a month, packed with the latest tutorials, delivered straight to your inbox.
We'll never send any spam or promotional emails.
Author

Hey, I'm Md Obydullah. I build open-source projects and write article on Laravel, Linux server, modern JavaScript and more on web development.

Follow