Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects



Sunday, 13 October 2013

Method Overloading in Java - Example and Explanation

On this particular blog we will come across a very useful and frequently used concept i.e. Method Overloading. We will see the conditions and scenarios of, 'usage of method overloading in java' and 'when it is overloading and when it is not'.

Overloading in java occurs when methods in a same class or in child classes shares a same name with a ‘difference in number of arguments’ or ‘difference in argument type’ or both.


How to achieve method overloading in java

Method overloading in Java occurs when two or more methods shares same name and fulfill at least one of the following condition.
1) Have different number of arguments.
2) Have same number of arguments but their types are different.
3) Have both different numbers of arguments with a difference in their types.


Method overloading in same class.

Overloading occurs in a single class when two or more methods shares a same name with a ‘difference in number of arguments’ or ‘difference in argument type’ or both.
package com.beingjavaguys.core;
import java.util.Date;

public class MethodOverloadingDemo {
 
 /*
  * Method Overloading occurs when methods are having same name, but a
  * difference in the number of their parameters or type of their parameters
  * or both.
  */
 public void getEmpName(int empId) {
    ......
 }

 public void getEmpName(String empName) {
    ......
 }

 public void getEmpName(int empId, String empName) {
    ......
 }

 public void getEmpName(Date dob, String empName) {
    ......
 }
}


Method overloading in subclass

It is also possible to overload methods in sub classes, as long as the methods shares same name and they are different in the number of parameters or type of parameters.
public class Fruit {
   public void getFruit(int fruitId){
      ......
   }
}

public class Apple extends Fruit {

   public void getFruit(String name) {
      ......  
   }
   public void getFruit(int fruitId, String name) {
      ......
   }

}


How to not overload method

Methods having same name in one class or in child class could not perform method overloading if they are just different in:
1) Name of argument, and not in number of arguments or type of arguments.
2) Having different return type only.
/*
  * Just a difference in name of parameters and not in their number or type
  * is not method overloading, it will give an compilation error
  */
 public void getEmpName(int empId) {
    ......
 }

 public void getEmpName(int employeeId) {
    ......
 }

/*
  * It is not method overloading with just difference in return type of
  * methods of having same name, and not different in number of paramets or
  * type of parameter.
  */
 public void getFruit(String name){
  ....
  
 }

 public String getFruit(String name){
  ....
  
  return "Apple";
 }


How method overloading helps in Java

When it comes to, why to use method overloading and usage of method overloading. Two points comes out
1) Helps in maintain consistency in method naming, doing same task with just a difference of input parameter.
2) Helps in reduce overhead of remember name of different functions for same task, one can pass possible different type of parameters or number of parameters to a single overloaded function to get the result.
/*
  * This is purely inconsistent and rubbish to have three different names of
  * methods for doing same task, all are just returning Employee.
  */
 public Employee getEmpById(int empId) {
  ......
 }

 public Employee getEmpByName(String empName) {
  ......
 }

 public Employee getEmpByDob(Date dob) {
  ......
 }

 /*
  * Its better to overload methods with same functionality, this make the
  * code consistent and reduce overhead of remember method names with
  * different type and number of input parameter. This way user just need to
  * pass available input parameter to the same method.
  */
 public Employee getEmp(int empId) {
  ......
 }
 public Employee getEmp(String empName) {
  ......
 }
 public Employee getEmp(Date dob) {
  ......
 }


Here we are done with 'Method Overloading in Java - Example and Explanation '. In our upcoming blogs we will see more about Java Programming and other opensource technologies.








Thanks for reading !
Being Java Guys Team




6 comments:

  1. Define Constructor Overloading. Give an example of Constructor Overloading (java)

    ReplyDelete
  2. Good work :) keep it up

    ReplyDelete
  3. give some realtime time examples....

    ReplyDelete
  4. tell about use of method overriding with real time examples

    ReplyDelete
  5. overloding real time example

    ReplyDelete

Like Us on Facebook


Like Us On Google+



Contact

Email: neel4soft@gmail.com
Skype: neel4soft