|
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);
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); } } |
|
See also Do you have a Java Problem?Ask It in The Java Forum Java Books
Return to : Java Programming Hints and Tips All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
|