0% found this document useful (0 votes)
137 views

Method Overloading

Method overloading in Java allows methods within a class to have the same name but different parameters. When an overloaded method is called, Java determines which version to execute based on the number and type of arguments passed in. Overloading supports polymorphism by implementing the concept of one interface with multiple methods. It is useful for writing methods that perform similar logic on different data types.

Uploaded by

Farisy Al-Bana
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views

Method Overloading

Method overloading in Java allows methods within a class to have the same name but different parameters. When an overloaded method is called, Java determines which version to execute based on the number and type of arguments passed in. Overloading supports polymorphism by implementing the concept of one interface with multiple methods. It is useful for writing methods that perform similar logic on different data types.

Uploaded by

Farisy Al-Bana
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

Method

Overloading

What is method overloading?

In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations are different

What is method overloading? (cont)

When this is the case, the methods are said to be overloaded, and the process is referred to as method overloading

How the overloading works?

When an overloaded method is invoked, Java uses the type and/or number of arguments as its guide to determine which version of the overloaded method to actually call.

How the overloading works? (cont)

When an overloaded method is invoked, Java uses the type and/or number of arguments as its guide to determine which version of the overloaded method to actually call. Then, overloaded methods must differ in the type and/or number of their parameters

How the overloading works? (cont)

While overloaded methods may have different return types; The return type alone is insufficient to distinguish two versions of a method. When Java encounters a call to an overloaded method, it simply executes the version of the method whose parameters match the arguments used in the call.

Example of overloaded code


OverloadedDemo.java

Example of overloaded code (cont)


Overload.java

Example of overloaded code (cont)

The output of the code:

Code description

As you can see, test( ) is overloaded four times. The first version takes no parameters, the second takes one integer parameter, the third takes two integer parameters, and the fourth takes one double parameter.

Code description (cont)

The fact that the fourth version of test( ) also returns a value is of no consequence relative to overloading, since return types do not play a role in overload resolution.

Why use method overloading?

Method overloading supports polymorphism because it is one way that Java implements the "one interface, multiple methods" paradigm

Why use method overloading? (cont)

In languages that do not support method overloading, each method must be given a unique name But, frequently you will want to implement essentially the same method for different types of data.

Why use method overloading? (cont)

For example consider the absolute value function; In languages that do not support overloading, there are usually three or more versions of this function, each with a slightly different name.

Why use method overloading? (cont)

For instance, in C,
1.
2.

3.

the function abs( ) - returns the absolute value of an integer labs( ) returns the absolute value of a long integer fabs() returns the absolute value of a floatingpoint value

Since C does not support overloading, each function has to have its own name, even though all three functions do essentially the same thing

Why use method overloading? (cont)

Indeed, Java's standard class library includes an absolute value method, called abs( ). This method is overloaded by Java's Math class to handle all numeric types. Java determines which version of abs() to call based upon the type of argument.

You might also like