Method overloading in Java

JavaJava Programming Java 8

Method overloading is a type of static polymorphism. In Method overloading, we can define multiple methods with the same name but with different parameters. Consider the following example program.

Example

 Live Demo

public class Tester {
   public static void main(String args[]) {
      Tester tester = new Tester();
      System.out.println(tester.add(1, 2));
      System.out.println(tester.add(1, 2,3));
   }
   public int add(int a, int b) {
      return a + b;
   }
   public int add(int a, int b, int c) {
      return a + b + c;
   }
}

Output

3
6

Here we’ve used a method add() which can take either two or three parameters and can acts accordingly. This is called method overloading or static polymorphism.

raja
Published on 02-Feb-2018 10:13:03
Advertisements