Concept of Overloading

Explain the concept of overloading with suitable example.

Overloading is a mechanism in which we can use many methods having the same function name but can pass different number of parameters or different types of parameter.

For Example:-

int sum(int a,int b);
double sum(double a,double b);
int sum(int 1,int b,int c);

That means, by overloading mechanism, we can handle different number of parameters or different types of parameter by having the same method name.

Following example explain the concept of overloading.

public class overDemo {

     public static void main(String args[]) {

           System.out.println(“Sum of two integer “);

           Sum(10,20);

           System.out.println(“Sum of two double numbers “);

           Sum(10.5,20.4);

           System.out.println(“Sum of three integer “);

           Sum(10,20,30);

     }

     public static void Sum(int num1,int num2) {

           int ans=num1+num2;

           System.out.println(ans);

      }

      public static void Sum(double num1,double num2) {

          double ans=num1+num2;

          System.out.println(ans);

      }

      public static void Sum(int num1,int num2,int num3) {

         int ans=num1+num2+num3;

         System.out.println(ans);

       }

}

Java Tips

See also

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.