Java Method Overloading. If you want to get knowledge and clear understanding about Method Overloading in Java. Then its a best place for you, here i will teach you in detail about Java Method Overloading with very easy to understand examples.
What is Method overloading?
In
Java, when we have more than one methods with the same name and having
different parameter list or different parameter types, than this concept is
known as method overloading. It can be achieved either by changing number of
parameters or by changing the type of parameters.
Example
int
a()
{
}
int
a(int x)
{
}
int
a(int y, int z)
{
}
All
above methods are overloaded methods in Java because of same name but having
different parameter quantity or type
Why we use Method overloading?
Whenever
we have to perform same type of task in Java using methods, it is better
approach to use same name and perform different tasks from those methods
instead of giving different names to different methods. Lets explain it using
simple example
Lets
suppose we have some values and need to perform calculation on them that can be
adding or subtracting etc
In
first case, we have 2 values to perform calculation and in second case we have
3 values and so on
Now
to perform this task, we have two options, One to create different methods with
different names and perform tasks, second is to create different methods with
the same name because of simplicity in our program
How to use Method overloading using Example Program?
pubic
class MethodOverloadingExample
{
{
int
a;
a
= x;
System.out.println(a);
}
public
void show(int y, int z)
{
int
b,c;
b
= y;
c
= z;
System.out.println(b
+ “ “ + c);
}
Public
static void main (String a[])
{
MethodOverloadingExample
obj = new MethodOverloadingExample ();
MethodOverloadingExample
obj2 = new MethodOverloadingExample ();
Obj1.show(100);
Obj2.show(200,300);
}
}
Output
will be:
100
200
300
Example
2
pubic
class MethodOverloadingExample2
{
{
int
a,b;
a
= x;
b
= y;
System.out.println(a
+ b); //performing addition here
}
public
void calculate (float m, float n)
{
float
k,l;
k
= m;
l
= n
System.out.println(k
* l); //performing multiplication here with same method
}
Public
static void main (String a[])
{
MethodOverloadingExample2
obj = new MethodOverloadingExample ();
MethodOverloadingExample2
obj2 = new MethodOverloadingExample ();
}
Obj.calculate(2,3);
Obj.calculate(1.2,3.4);
Output
will be:
5
4.5
Example 3
pubic
class MethodOverloadingExample3
{
public
void show(int x)
{
int
a;
a
= x;
System.out.println(a);
}
public
void show (String x)
{
String
name;
name
= x;
System.out.println(name);
}
Public
static void main (String a[])
{
MethodOverloadingExample3
obj1 = new MethodOverloadingExample3 ();
MethodOverloadingExample3
obj2 = new MethodOverloadingExample3 ();
Obj1.show(100);
Obj2.show(“Java”);
}
}
Output
will be:
100
Java
In
the above example, we have show method which is accepting int type of parameter
and string type of parameter. And when objects are created, than at that time,
we are sending int and string type of values to the Java methods, and as the
argument types are different, so that is why both will be called according to
the parameter type

No comments:
Post a Comment