Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
164 views

Method Overloading in Java PDF

Method overloading in Java allows a class to have multiple methods with the same name but different parameters. This increases readability by allowing related operations to be grouped under the same method name. Method overloading can be achieved by changing the number of parameters or their data types. For example, a class could define methods named "add" that perform addition with two integers, three integers, two doubles, etc. The compiler determines which overloaded method to call based on the number, types, and order of the arguments passed.

Uploaded by

Karthick
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
164 views

Method Overloading in Java PDF

Method overloading in Java allows a class to have multiple methods with the same name but different parameters. This increases readability by allowing related operations to be grouped under the same method name. Method overloading can be achieved by changing the number of parameters or their data types. For example, a class could define methods named "add" that perform addition with two integers, three integers, two doubles, etc. The compiler determines which overloaded method to call based on the number, types, and order of the arguments passed.

Uploaded by

Karthick
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Method Overloading in Java

If a class has multiple methods having same name but different in parameters, it is known as Method
Overloading.

If we have to perform only one operation, having same name of the methods increases the readability
of the program.

Suppose you have to perform addition of the given numbers but there can be any number of
arguments, if you write the method such as a(int,int) for two parameters, and b(int,int,int) for three
parameters then it may be difficult for you as well as other programmers to understand the behavior
of the method because its name differs.

Advantage of method overloading

Method overloading increases the readability of the program.

Different ways to overload the method

There are two ways to overload the method in java

1. By changing number of arguments

2. By changing the data type

1) Method Overloading: changing no. of arguments

In this example, we have created two methods, first add() method performs addition of two numbers
and second add method performs addition of three numbers.

In this example, we are creating static methods so that we don't need to create instance for calling
methods.

class Adder{

static int add(int a,int b){return a+b;}

static int add(int a,int b,int c){return a+b+c;}

class TestOverloading1{

public static void main(String[] args){

System.out.println(Adder.add(11,11));

System.out.println(Adder.add(11,11,11));

}}

Output:
22

33

2) Method Overloading: changing data type of arguments

In this example, we have created two methods that differs in data type. The first add method receives
two integer arguments and second add method receives two double arguments.

1. class Adder{

2. static int add(int a, int b){return a+b;}

3. static double add(double a, double b){return a+b;}

4. }

5. class TestOverloading2{

6. public static void main(String[] args){

7. System.out.println(Adder.add(11,11));

8. System.out.println(Adder.add(12.3,12.6));

9. }}
Output:

22

24.9

Method Overloading

With method overloading, multiple methods can have the same name with different parameters

Example

int myMethod(int x)

float myMethod(float x)

double myMethod(double x, double y)

Consider the following example, which has two methods that add numbers of different type:

Example

static int plusMethodInt(int x, int y) {

return x + y;

static double plusMethodDouble(double x, double y) {

return x + y;
}

public static void main(String[] args) {

int myNum1 = plusMethodInt(8, 5);

double myNum2 = plusMethodDouble(4.3, 6.26);

System.out.println("int: " + myNum1);

System.out.println("double: " + myNum2);

Instead of defining two methods that should do the same thing, it is better to overload one.

In the example below, we overload the plusMethod method to work for both int and double:

Example

static int plusMethod(int x, int y) {

return x + y;

static double plusMethod(double x, double y) {

return x + y;

public static void main(String[] args) {

int myNum1 = plusMethod(8, 5);

double myNum2 = plusMethod(4.3, 6.26);

System.out.println("int: " + myNum1);

System.out.println("double: " + myNum2);

Method overloading is a concept that allows to declare multiple methods with same name but different
parameters in the same class.

Java supports method overloading and always occur in the same class(unlike method overriding).

Method overloading is one of the ways through which java supports polymorphism. Polymorphism is a
concept of object oriented programming that deal with multiple forms.

Method overloading can be done by changing number of arguments or by changing the data type of
arguments.
If two or more method have same name and same parameter list but differs in return type can not be
overloaded.

Note: Overloaded method can have different access modifiers and it does not have any significance in
method overloading.

Method overloading and null error:

This is a general issue when working with objects, if same name methods having reference type
parameters then be careful while passing arguments.

Below is an example in which you will know how a null value can cause an error when methods
are overloaded.

Example:

Null value is a default value for all the reference types. It created ambiguity to JVM that reports
error.

public class Demo

public void test(Integer i)

System.out.println("test(Integer ) ");

public void test(String name)

System.out.println("test(String ) ");

public static void main(String [] args)

Demo obj = new Demo();

obj.test(null); }}

You might also like