Header Ads

  • Breaking Now

    What is method overloading?

    A method with changed formal parameters will lead to implementing method overloading.

    int calculateSum(int i,int j)
    float calculateSum(float i,int j)
    double calculateSum(double i,int j)
    float calculateSum(int i,float j)

    5 comments:

    1. http://stackoverflow.com/questions/1696314/java-different-return-types-when-overloading

      Check this link. The examples that you have given here are bad. You can not change the method return types (unless the return type follows inheritance itself) while overloading. Overloading means changing the parameter types or number of parameters. If you can recall, checked exceptions in the throws clause of the methods follow the hierarchy in overriding.

      ReplyDelete
    2. It is still debated as per the post...thx for your comments Anoop.

      ReplyDelete
    3. Hi,

      I tried int calculatesum()
      and string calculatesum(), the compiler complains, calculatesum is already declared but when I try
      int calculatesum(int i, float j) and int calculateSum(int i,int j) it works, so I am not sure how overloading really works.

      ReplyDelete
    4. You can still change a method return type as long as the method signature is/are different.Overloaded methods are differentiated by the number and the type of the arguments passed into the method. http://download.oracle.com/javase/tutorial/java/javaOO/methods.html

      ReplyDelete
    5. method overloading means, method should be same but parameter type/number of parameters and return type should be different

      ReplyDelete

    Post Top Ad

    Post Bottom Ad