CodeGym /Java Blog /Methods in Java /Method Overloading in Java
Author
Pavlo Plynko
Java Developer at CodeGym

Method Overloading in Java

Published in the Methods in Java group
The Java programming language strutted onto the scene fully embracing the concept of Object-Oriented Programming (OOP). And in the OOP one of the groovy moves is Polymorphism. What's that, you ask? It's a fancy way of saying that objects and methods in your program can boogie in different ways depending on the context. In plain talk, a method with the same name can do different actions and pull out diverse results, all based on its parameters. So, let's dive into the world of Method overloading in Java and see what kind of programming magic we can conjure!

What is method overloading

In Java, method overloading, as we discussed earlier, is a part of the polymorphism concept. This feature allows a class to have more than one method with the same name, as long as the parameters are different. The difference in parameters can be in terms of the number of parameters or the type of parameters. This provides the flexibility to call a similar method for different types of data.

Example of Method Overloading

Let’s have a simple example to illustrate method overloading. Say, we're about to program a calculator and the first step is to create a numbers addition function. Now, we could conjure a method named 'add' for two integers... or, just for a twist, we could whip up another 'add' for doubles, to gracefully handle those tricky non-integer numbers. Also let’s create a method ‘add’ to add three integer numbers… just for our idea of overloading to demonstrate. Here's how it might look in our Java magic show:

public class OverloadingExample {


       // Method to add two integers
       public int add(int a, int b) {
           return a + b;
       }


       // Overloaded method to add two double values
       public double add(double a, double b) {
           return a + b;
       }


       // Overloaded method to add three integers
       public int add(int a, int b, int c) {
           return a + b + c;
       }


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


           // Using the first add method
           System.out.println("Sum of two integers: " + example.add(10, 20));


           // Using the second add method
           System.out.println("Sum of two doubles: " + example.add(10.5, 20.5));


           // Using the third add method
           System.out.println("Sum of three integers: " + example.add(10, 20, 30));
       }
}
The output of this program is:
Sum of two integers: 30 Sum of two doubles: 31.0 Sum of three integers: 60
This is how method overloading is implemented in Java, allowing methods to be called with different types and numbers of arguments.

Method Overloading vs Method Overriding – What's the Difference?

In Java, besides method overloading, there is also method overriding, which is very similar to it. The latter is also related to polymorphism, but they serve different purposes and have different rules. It is also connected with another important OOP concept - inheritance. So method overriding is a feature that allows a subclass to provide a specific implementation for a method that is already defined in its parent class. This is a fundamental part of the inheritance concept in object-oriented programming, allowing for runtime polymorphism. Here is a small code example. Let's create a class 'MusicalInstrument' and two subclasses: 'Piano' and 'Violin'. Both instruments are meant for playing, but the playing technique differs for each. We will override the parent method play() in both subclasses to demonstrate the concept.

class MusicalInstrument {
 void play() {
 System.out.println("Playing a musical instrument");
 }
}

class Violin extends MusicalInstrument {
 @Override
 void play() {
 System.out.println("Playing the violin");
 }
}

class Piano extends MusicalInstrument {
 @Override
 void play() {
 System.out.println("Playing the piano");
 }
}
public class Main {
 public static void main(String[] args) {
 MusicalInstrument myViolin = new Violin();
 MusicalInstrument myPiano = new Piano();

 myViolin.play(); // Outputs: Playing the violin
 myPiano.play(); // Outputs: Playing the piano
 }
}
In both subclasses (Violin and Piano), the play method is overridden to provide specific behavior for each instrument. You can see the @Override annotation. It’s not a necessity, but it indicates that the method overrides a method of the superclass. At runtime, the JVM determines which specific play method to call based on the actual object type. It is called dynamic polymorphism. At the same time, method overloading represents Compile-time Polymorphism. Overloading is resolved during compile time, which is why it's also known as static polymorphism. Let's sum it up, what's the difference between Method Overloading and Method Overriding:
  • Overloading is done within the same class; overriding involves a subclass and its superclass.
  • In overloading, methods must have different parameters. In overriding, the method signature (name and parameters) must be the same.
  • Overloaded methods are accessed based on the reference type, whereas overridden methods are accessed based on the object type.
  • In method overriding, you can call the parent class method in the overriding method using the super keyword. This is not applicable in overloading.
  • Compile-time Polymorphism is realized in overloading, while overriding is determined at runtime.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION