Method Overloading
Tccicomputercoaching.com
Java is a
high-level programming language originally developed by Sun Microsystems and
released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS,
and the various versions of UNIX. This tutorial gives a complete understanding
of Java.
Java
supports object oriented concepts like inheritance, Polymorphism, Data
Abstraction, Encapsulation etc…….
Polymorphism
means more than one form. It contains method overloading in Java.
What is
Method Overloading?
If a class
have multiple methods by same name but different parameters, it is known
as Method Overloading.
Why do we
use 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 behaviour of the method because its name
differs. So, we perform method overloading to figure out the program quickly.
But there
are some rules to be followed:
Must change
the argument list
Can change
the return type
Can change
the access modifier
Can declare
new or broader checked exception
Different
ways to overload the method
There are two ways to overload the method
in java
|
·
By changing number of arguments
·
By changing the data type
|
Example of
Method Overloading by changing no of arguments:
public void
add(inta, intb)
{
System.out.println("In
method with two argument");
}
//Overloaded
method with one argument
public void add(inta) {
System.out.println("In
method with one argument");
}
How to run method
overloading?
add(3,5);
add(10);
First method
with two arguments calls first add (3, 5) method. Second method with one
argument calls second method (10).
Example of
Method overloading by different data types:
class Calculation2
{
void sum(int a,int b)
{System.out.println(a+b);}
void sum(double a,double b)
{System.out.println(a+b);}
public static void main(String args[]){
Calculation2 obj=new Calculation2();
obj.sum(10.5,10.5);
obj.sum(20,20);
}
}
Output: 21.0
40
Example3:
Overloading – Sequence of data type of arguments
Here
method disp() is overloaded based on sequence of data type of
arguments – Both the methods have different sequence of data type in argument
list. First method is having argument list as (char, int) and second is having
(int, char). Since the sequence is different, the method can be overloaded
without any issues.
class
DisplayOverloading3
{
public void
disp(char c, intnum)
{
System.out.println("I’m
the first definition of method disp");
}
public void
disp(intnum, char c)
{
System.out.println("I’m
the second definition of method disp" );
}
}
In short,
Method Overloading increases the readability of programme.
If
you like this post then please share and like this post.
Call
us @ 98256 18292.
Visit
us @ tccicomputercaoching.com
No comments:
Post a Comment