Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Saturday, 11 February 2017

Method Overloading in Java

 

Method Overloading

Method overloading is a feature that allows a class to have two or more methods having same name, but different number of parameters or different types(Data types) of parameters or different order of parameters, It is known as Method Overloading.

Method overloading is also known as static polymorphism. We can achieve method overloading by using this static polymorphism. Method overloading is done at compile time. The best example of method overloading in the world is System.out.println() method which is overloaded to accept all kind of data types in java. There are println() method which takes int, float, double, String etc.


Method Overloading In Java



Why use method overloading in java?

Suppose we have to perform addition of given number but there can be any number of arguments, if we write method such as x(intint) for two arguments and y(int, int, int) for three arguments than it is very difficult for your and other programmers to understand purpose or behavior of method, they can't identify purpose of method. So we use method overloading concept to easily figure out the program.

for example above two methods x and y we can write sum(int, int) and sum(int, intint) using method overloading concept.


Advantage of method overloading in java

  • Method Overloading increases the readability of program.
  • Overloaded method are fast because they are bounded during compile time and no check or binding is required at run time.


Different ways to Overload the method

  • By changing number of arguments or parameters.
  • By changing the data type.

👉 : Method Overloading in java is not possible by changing the return type of the method.

Syntax 1:

class ChangingNoOfArguments
{
returnType methodName()//There is no arguments in method
{
--------
--------
}
returnType methodName(datatype1 variable1)//There is 1 argument 
{
--------
--------
}
returnType methodName(datatype1 variable1, datatype2 variable2)
{
-------
-------
}
}

Syntax 2:

class ChangingDataType
{
retrunType methodName(datatype1 variable1)//e.g methodName(int i)
{
-------
-------
}
retrunType methodName(datatype2 varibale2)//e.g mName(float f) 
{
-------
-------
}
}


1) Example Method Overloading by changing no. of arguments

Now here, we have created two overloaded methods first method 'sum' performs addition of two numbers and second method 'sum' performs addition of three numbers.

class Addition
{
void sum(int a, int b)
{
System.out.println(a+b);
}
void sum(int a, int b, int c)
{
System.out.println(a+b+c);
}
public static void main(String args[])
{
Addition a = new Addition();
a.sum(10,20);
a.sum(20,30,40);
}
}

output: 30
              90

Another example of method overloading by changing no. of arguments. here in this example we have created two methods, first method 'sum' perform addition of two number and second method 'sum' perform addition on three number but here we have created static methods so that we don't need to create instance to calling methods.

class Addition
{
static int sum(int a, int b)
{
return a+b;
}
static int sum(int a, int b, int c)
{
return a+b+c;
}
}

class Overloading1
{
public static void main(String args[])
{
System.out.println(Addition.sum(10,20));
System.out.println(Addition.sum(100,200,300));
}
}

save Addition.java
javac Additon.java(compile)
java Overloading1(run)

output: 30
              600


2) Example Method Overloading by changing the data type of argument

In this example, we have created two overloaded methods that differs int data types. The first method 'sum' receives two integer arguments and the second method 'sum' receives two float arguments.

class Addition
{
void sum(int a, int b)
{
System.out.println(a+b);
}
void sum(float a, float b)
{
System.out.println(a+b);
}
public static void main(String args[])
{
Addition a = new Addition();
a.sum(10,20);
a.sum(10.5,20.5);
}
}

output: 30
              31

Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate