Core Java

Overloading Java Methods Example

In this post, we feature a comprehensive article on Overloading Java Methods. Overloaded methods are methods with the same name signature but either a different number of parameters or different types in the parameter list. It is similar to constructor overloading, that allows a class to have more than one constructor having different argument lists. Method overloading is an example of Static Polymorphism also known as compile time binding or early binding where binding of method call to its definition happens at compile time.

1. Method Overloading

Let’s look at an example of method overloading. We have created a class, that has methods with the same names but with different arguments and use them in new class instances to see their behavior.

Overloading Example

public class OverloadingDemo {
    public static void main(String[] args) {
        Calculator calculator = new Calculator();
        System.out.println(calculator.add(2, 3));
        System.out.println(calculator.add(2.0, 3.0));
    }
}

class Calculator {
    int add(int a, int b) {
        return a + b;
    }

    double add(double a, double b) {
        return a + b;
    }
}
  • We create a simple class Calculator which has only the default constructor.
  • We have a add method to add two integers.
  • To extend the functionality of calculator to add two floats or doubles, another add method is supplied with doublearguments.
  • Calculator class is initialized with the values to demonstrate the example.

Output

5
5.0

 To the external world, there is only one add method which handles different inputs. Internally we can support multiple different behaviors based on input and argument types.

Considering the above, we can add one more variant of method overloading.

int add(int a, int b, int c) {
        return a + b + c;
 }

System.out.println(calculator.add(2, 3, 4));

Adding the above method, gives the flexibility to add three numbers. Here with the same method name we get different results.

9

2. Difference with Overriding

Method overriding allows a subclass to provide a specific implementation of a method that is already provided by one of its parent classes. The implementation in the subclass overrides the implementation in the super class by providing a method that has the same name, same parameters or signature, and same return type as the method in the parent class. The version of a method that is executed will be determined by the object that is used to invoke it. Method overriding is an example of Dynamic Polymorphism also known as Runtime binding or late binding where binding of method call to its definition happens during run time. Let us consider an example to illustrate

Overriding Example

class Calculator {
   ...
    double pow(double a, double b) {
        return 0;
    }
}

class ScientificCalculator extends Calculator {
    @Override
    double pow(double a, double b) {
        return Math.pow(a, b);
    }
}

public class OverloadingDemo {
    public static void main(String[] args) {
        Calculator calculator = new Calculator();
        System.out.println(calculator.pow(2.0, 3.0));
        calculator = new ScientificCalculator();
        System.out.println(calculator.pow(2.0, 3.0));
    }
}
  • Add a pow method in Calculator class to raise power of a to b.
  • Since this is normal calculator it just returns zero
  • We define a ScientificCalculator which does the actual power function.
  • Calling pow of parent class calculator will return 0 while child class will return the correct result

3. Download the Source code

This was an example of Overloading Java Methods.

Download
You can download the full source code of this example here: Overloading Java Methods Example

Rajagopal ParthaSarathi

Rajagopal works in software industry solving enterprise-scale problems for customers across geographies specializing in distributed platforms. He holds a masters in computer science with focus on cloud computing from Illinois Institute of Technology. His current interests include data science and distributed computing.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button