Method Overloading sample in Java
By: Ganesh Iyer
In same class, if name of the method remains common but the number and type of parameters are different, then it is called method overloading in Java.
class
overLoading
{
public static void main(String[] args)
{
functionOverload obj = new functionOverload();
obj.add(1,2);
obj.add(\"Life at \", \"?\");
obj.add(11.5, 22.5);
}
}
class functionOverload
{
/* void add(int a, int b)
// 1 - A
method with two parameters
{
int sum = a + b;
System.out.println(\"Sum of a+b
is \"+sum);
} */
void add(int a, int b, int c)
{
int sum = a + b + c;
System.out.println(\"Sum of
a+b+c is \"+sum);
}
void add(double a, double b)
{
double sum = a + b;
System.out.println(\"Sum of a+b
is \"+sum);
}
void add(String s1, String s2)
{
String s = s1+s2;
System.out.println(s);
}
}
Archived Comments
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Java program to get location meta data from an image
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program