CodesJava

Easy learning with example program codes

Method overloading in java


Method overloading is the way of implementing static/compile time polymorphism in java. Method overloading means more than one methods in a class with same name but different parameters. Parameters can be differing in types, numbers or order. Compiler resolve method call by matching method signature at compile time, that’s why it is known as static or compile time polymorphism. It is also known as static binding.

Ways to implement method overloading in java:

  1. Parameters differ in types.
  2. Parameters differ in number.
  3. Parameters differ in order.

1. Parameters differ in types.

Example:

Below example have two methods which have the same name bur method parameters are differ in their order.
AddExample.java

/**
 * This class is used for method overloading
 * by parameters change in their types.
 * @author CodesJava
 */
public class AddExample {
      /**
       * This method is used to add two integer values.
       * @param var1
       * @param var2
       * @author CodesJava
       */
      void add(int var1, int var2){
            System.out.println(var1 + var2);
      }     
 
      /**
       * This method is used to add one double and one integer.
       * @param var1
       * @param var2
       * @author CodesJava
       */
      void add(double var1, int var2){
            System.out.println(var1 + var2);
      }     
 
      /**
       * This method is used to concatenate two string values.
       * @param var1
       * @param var2
       * @author CodesJava
       */
      void add(String var1, String var2){
            System.out.println(var1 + var2);
      }     
 
      public static void main(String args[]){
            //creating object here
            AddExample addExample = new AddExample();
            //method call
            addExample.add(10, 20);
            addExample.add(12.50, 30);
            addExample.add("hello ", "java.");
      }
}

Output:

 30
 42.5
 hello java.

Download this example.

 2. Parameters differ in number.

Example:

Below example have two methods which have the same name but method parameters are differ in number.
AddExample.java

/**
 * This class is used for method overloading
 * by parameters change in number.
 * @author CodesJava
 */
public class AddExample {
      /**
       * This method is used to add two integer values.
       * @param num1
       * @param num2
       * @author CodesJava
       */
      void add(int num1, int num2){
            System.out.println(num1 + num2);
      }     
 
      /**
       * This method is used to add three integer values.
       * @param num1
       * @param num2
       * @param num3
       * @author CodesJava
       */
      void add(int num1, int num2, int num3){
            System.out.println(num1 + num2 + num3);
      }           
 
      public static void main(String args[]){
            //creating object here
            AddExample addExample = new AddExample();
            //method call
            addExample.add(10, 20);
            addExample.add(20, 30, 40);
      }
}

Output:

30
90

Download this example.

 3. Parameters differ in order.

Example:

Below example have two methods which have the same name bur method parameters are differ in their order.
AddExample.java

/**
 * This class is used for method overloading
 * by parameters change in order.
 * @author CodesJava
 */
public class AddExample {
      /**
       * This method is used to add two numerics.
       * @param num1
       * @param num2
       * @author CodesJava
       */
      void add(int num1, double num2){
            System.out.println(num1 + num2);
      }  
 
      /**
       * This method is used to add two numerics.
       * @param num1
       * @param num2
       * @author CodesJava
       */
      void add(double num1, int num2){
            System.out.println(num1 + num2);
      }           
 
      public static void main(String args[]){
            //creating object here
            AddExample addExample = new AddExample();
            //method call
            addExample.add(10, 20.40);
            addExample.add(20.50, 30);
      }
}

Output:

30.4
50.5

Download this example.

Why method overloading is not possible by changing return type of the method?

Method overloading is not possible by changing return type of the method. Because, as discussed above compiler resolve method call by matching method signature(method name and parameters). If method signatures are same for two or more methods then how compiler will know which method have to be called.
AmbiguityInOverloading.java

/**
 * This class is used to show ambiguity problem
 * in case of change in return type of the method.
 * @author CodesJava
 */
public class AmbiguityInOverloading {
      /**
       * This method is used to add two numerics.
       * @param num1
       * @param num2
       * @return int
       * @author CodesJava
       */
      int add(int num1, int num2){
            return num1+num2;
      }     
 
      /**
       * This method is used to add two numerics.
       * @param num1
       * @param num2
       * @return float
       * @author CodesJava
       */
      float add(int num1, int num2){
            return num1 + num2;
      }           
 
      public static void main(String args[]){
      //creating object here
      AmbiguityInOverloading obj = new AmbiguityInOverloading();
      //compiler can't differentiate method call
      System.out.println(obj.add(10, 20));
      }
}

Output:

Exception in thread "main" java.lang.Error: 
Unresolved compilation problem:
The method add(int, int) is undefined for the type AmbiguityInOverloading
at com.javawithease.business.AmbiguityInOverloading.main
(AmbiguityInOverloading.java:34)

Download this example.

 Note: main method can also be overloaded.

MainMethodOverloding.java

/**
 * This class is used for main method overloading.
 * @author CodesJava
 */
public class MainMethodOverloding {
      //overloaded main method with one parm
      public static void main(int num1){
            System.out.println(num1);    
      }
 
      //overloaded main method with two parm
      public static void main(int num1, int num2){
            System.out.println(num1 + num2);
      }
 
      public static void main(String args[]){
            //method call
            main(20);
            main(10, 20);
      }
}

Output:

20
30

Download this example.
 
Next Topic: Method overriding in java with example.
Previous Topic: Polymorphism in java.

Related Topics:

Method overloading in java with example.
Method overriding in java with example.
Dynamic method dispatch or Runtime polymorphism in java with example.
Association in java with example.
Inheritance in java with examples.
Aggregation in java with example.

 

Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
JVM, JRE and JDK.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial
Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki