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.
Methods are used in Java to describe the behavior of an
object. Methods are a collection of statements that are group together to
operate. In Java, it is possible to create methods that have the same name, but
different argument lists in various definitions, i.e., method overloading is
possible in Java, which is one of the unique features of Object Oriented
Programming (OOP).
an example where you
can perform multiplication of given numbers, but there can be any number of
arguments a user can put to multiply, i.e., from the user point of view the
user can multiply two numbers or three numbers or four numbers or so on. If you
write the method such as multi(int, int) with two parameters for multiplying
two values, multi(int, int, int), with three parameters for multiplying three
values and so on. A programmer does method overloading to figure out the
program quickly and efficiently. Method Overloading is applied in a program
when objects are required to perform similar tasks but different input
parameters. Every time an object calls a method, There are different ways to
overload a method in Java. They are:
1. Based on the number of
parameters: In this case, the entire overloading concept depends on the
number of parameters that are placed within the parenthesis of the method.
2. Based on the data type of
the parameter: With the arrangement of data type, within the parameter,
overloading of the method can take place.
3. Based on the sequence of
data types in parameters: The method overloading also depends on the
ordering of data types of parameters within the method.
4. To create overloaded
methods, programmers need to develop several different method definitions in
the class, all have the same name, but the parameters list is different. Here
is a sample code snippet:
method overriding in java |
Advantage of method Overloading
·
It
is used to perform a task efficiently with smartness in programming.
·
It
increases the readability of the program.
·
The
Method overloading allows methods that perform proximately related functions to
be accessed using a common name with slight variation in argument number or
types.
·
They
can also be implemented on constructors allowing different ways to initialize
objects of a class
Disadvantage of method Overloading
·
It's esoteric. Not very easy for the beginner
to opt this programming technique and go with it.
·
It requires more significant effort spent on
designing the architecture (i.e., the arguments' type and number) to up front,
at least if programmers' want to avoid massive code from rewriting.
Syntax:
public class Student {
public void add(int i, int j) {
....
}
public void add(int i) {
....
}
}
Example:
class
Multiply {
void mul(int a, int b) {
System.out.println("Sum of
two=" + (a * b));
}
void mul(int a, int b, int c) {
System.out.println("Sum of
three=" + (a * b * c));
}
}
public
class Polymorphism {
public static void main(String args[]) {
Multiply m = new Multiply();
m.mul(6, 10);
m.mul(10, 6, 5);
}
}
Output:
Sum of two=60
Sum of three=300
No comments:
Post a Comment