Overloading in java

An Overloaded Method gives you an option to use the same Method name in a Class but with a different argument.

Overloading in Java Method Rules

There are some rules associated with an overloaded Method.

Overloaded methods

  • Must change the argument list
  • Can change the return type
  • Can change the access modifier(Broader)
  • Can declare new or broader checked exception
A method can be overloaded in Class or in SubClass.

 

Overloading Method Example

        //Overloaded method with one argument
	public void add(int input1, int input2) {
		System.out.println("In method with two argument");
	}

        //Overloaded method with one argument
	public void add(int input1) {
		System.out.println("In method with one argument");
	}

 

Invoking Overloaded Method

Out of several available overloaded Methods the method invoked is based on the arguments.

     add(3,4);
     add(5);

First call will execute the first Method and second will execute the second Method.

The reference type decides which overloaded Method to invoke and not the Object as opposed to Overriding the Method.

Thanks to Manoj for pointing typo error.

Overloading Cheatsheet

  • Using the same Method name but with different argument is called overloading.
  • Constructors can also be overloaded
  • Overloaded Methods must have different argument set.
  • Overloaded Methods may have different return type.
  • Overloaded Methods may have different access modifier.
  • Overloaded Methods may throw different exception broader or narrow no restriction
  • Methods from super class can also be overloaded in subclass.
  • Polymorphism applies to overriding not Overloading
  • Determining which overloaded Method will be invoked is decided at compile time on the basis of the reference type.
Series Navigation<< Interface in javaJava Method Override >>
By | 2017-07-09T12:11:41+00:00 April 21st, 2012|Core Java|10 Comments

About the Author:

10 Comments

  1. Ankan April 23, 2013 at 7:33 am - Reply

    “Can declare new or broader checked exception” can you examplify this point…

  2. manoj November 7, 2013 at 8:02 am - Reply

    Overloaded methods

    Wrong : Must not change the argument list
    Correct : Must change the argument list

    • Vivekanand Gautam November 11, 2013 at 5:29 pm - Reply

      Hi Manoj,

      Thanks for pointing the error on Overloading article. Same has been resolved. But i could not understand your other point “If method can be overriden then its access modifier is changable as well.” Please elaborate so that i can change the article if required.

      Also if you want to share your knowledge with others by writting article on Java related topic do let me know.

      Thanks

  3. RAHUL KIRTE August 1, 2014 at 1:32 am - Reply

    I is very nice but i want all notes related subject
    can u help me….
    mo:- 7507827987

  4. neetu December 15, 2014 at 5:50 am - Reply

    nice explain of overloading method

  5. Shivarajan s March 20, 2015 at 4:43 pm - Reply

    Not visible code within bracket what can i do

  6. Yamini Sukhija June 15, 2015 at 10:58 am - Reply

    REFERENCE TYPE DECIDED WHICH OVERLOADED METHOD TO INVOKE AND NOT THE OBJECT AS OPPOSED TO OVERRIDING.

    What does this mean?
    Can you please elaborate.

    • V Gautam June 15, 2015 at 4:32 pm - Reply

      In order to understand this line you need to understand reference and object.

      Test obj = new Test();

      In above line obj is a reference which is actually pointing to a Object of Test class.
      Now if u know reference can hold child object too.

      Class TestChild extends Test{}

      Test obj1 = new TestChild();

      Above line is true too.
      But in this case reference which is obj1 is of type Test while actual object will be of type TestChild. Which will be known only at runtime and not at compile time.

      So now read that line again. Which overloaded method will be called will be determined by reference type(Obj /obj1) and not the object type(Test / TestChild).

      This is opoosed to Overrding. In which case actual object will determin which method to invoke and as u know actual object type can be determined only at runtime hence it is called runtime binding while Overloding called compile time binding.

      Read article after read it will clear your doubt.

      Hope it help.

      • Mahendra Kamble January 8, 2016 at 9:10 am - Reply

        nice explanation!!
        thanx

  7. Anurag Singh July 28, 2017 at 11:08 am - Reply

    this article is very clean and simple.
    so thanks

Leave A Comment