What is method overloading in Java?

Java 8Object Oriented ProgrammingProgramming

When a class has two or more methods by the same name but different parameters, at the time of calling based on the parameters passed respective method is called (or respective method body will be bonded with the calling line dynamically). This mechanism is known as method overloading.

If you observe the following example, Here we have created a class named Sample and this class has two methods with same name (add) and return type, the only difference is the parameters they accept (one method accepts two integer variables and other accepts three integer variables).

When you invoke the add() method based on the parameters you pass respective method body gets executed.


Live Demo

public class Sample{
   public static void add(int a, int b){
   public static void add(int a, int b, int c){
   public static void main(String args[]){
      Sample obj = new Sample();
      obj.add(20, 40);
      obj.add(40, 50, 60);


Published on 08-Jan-2018 08:05:19