Method Overloading
In previous post we already discuss about on Syntax Of Method in java. Now we learn about on the Method overloading in java.
A method is said to be overloaded if and only if Method Name is Same but Signature is different . This is also known as Method Overloading. Here Signature represents the following points but one thing must be different i.e..
A method is said to be overloaded if and only if Method Name is Same but Signature is different . This is also known as Method Overloading. Here Signature represents the following points but one thing must be different i.e..
a) Number of parameters
b) Type of parameters
c) Order of parameters
The purpose of method overloading concept is mainly used on the development of constructor in java. So this concept must be followed by methods for developing the Method overloading and Method overriding. Consider the following example show on the Method Overloading concept.
sum(10);
sum(10,20);
sum(10,20,30);
sum(10.5f);
sum(10.5f,20.5f);
sum(java,kvr);
sum(java4teachus,Lucky);
Definition
In java programming it is possible to define the two or more methods within the same class with the same class. But the parameters declarations are different, this process is referred to as "Method Overloading". Method overloading is one of the way to achieve polymorphism in java. Overloaded method must be differ in the type or number of their parameters.
Example:-
class OverloadDemo
{
void test()
{
System.out.println("No Parameters");
}
void test(int a)
{
a=10;
System.out.println("value of a="+a);
}
void test(int a, int b)
{
a=10;
b=20;
System.out.println("value of a="+a);
System.out.println("value of b="+b);
}
void test(float a)
{
a=20.1f;
System.out.println("value of a="+a);
}
void test(float a, float b)
{
a=20.0f;
b=20.3f;
System.out.println("value of a="+a);
System.out.println("value of b="+b);
}
void test(char a, char b, char c, char d, char e)
{
a='L';
b='U';
c='C';
d='K';
e='Y';
System.out.println("value of a="+a);
System.out.println("value of b="+b);
System.out.println("value of c="+c);
System.out.println("value of d="+d);
System.out.println("value of e="+e);
}
}
⏩ Constructor Overloading is a kind of Method Overloading. This concept is useful when objects are required to perform the similar tasks but using different parameters.
⏩ Constructor Overloading is having any number of constructors provided they take different number or type of arguments.
⏩ The following program has three constructors that demonstrate constructor overloading.
Example:-
class Box
{
private int l,b,h;
box()
{
l=b=h=0;
}
box()
{
l=b=h=x;
}
box(int ll, int bb, int hh)
{
l=ll;
b=bb;
h=hh;
}
}
Explanation:- In the above example, there are three constructors called constructor without arguments, constructor with one argument, constructor with three arguments.
No comments:
Post a Comment
Thanks for visiting my site..