Overloading in java

This entry is part 19 of 34 in the series Core Java Course

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

 

Invoking Overloaded Method

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

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 >>

9 Comments Overloading in java

  1. manoj

    Overloaded methods

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

    Reply
    1. Vivekanand Gautam

      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

      Reply
  2. Yamini Sukhija

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

    What does this mean?
    Can you please elaborate.

    Reply
    1. V Gautam

      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.

      Reply

Leave A Comment

Your email address will not be published. Required fields are marked *