If two or more method in a class has same name but different
parameters, it is known as method overloading.
Method overloading can be done by changing number of arguments or
by changing the data type of arguments.
If two or more method have same name and same parameter list but
differs in return type are not said to be overloaded method.
Different ways to overload the method
- By changing number of arguments or parameters.
- By changing the data type.
- Sequence of Data type of parameters.
When an overloaded method is called the compiler looks for exact
match.
Sometime when exact match is not found, java automatic type conversion
plays a vital role
Example for By changing number of arguments or parameters.
class ChangeArgumentList
{
void find(int l, int b)
{
System.out.println("Area is"+(l*b)) ;
}
void find(int l, int b,int h)
{
System.out.println("Area is"+(l*b*h));
}
public static void main (String[] args)
{
ChangeArgumentList ar = new ChangeArgumentList();
ar.find(8,5); //find(int l, int b) is method is called.
ar.find(4,6,2); //find(int l, int b,int h) is called.
}
}
{
void find(int l, int b)
{
System.out.println("Area is"+(l*b)) ;
}
void find(int l, int b,int h)
{
System.out.println("Area is"+(l*b*h));
}
public static void main (String[] args)
{
ChangeArgumentList ar = new ChangeArgumentList();
ar.find(8,5); //find(int l, int b) is method is called.
ar.find(4,6,2); //find(int l, int b,int h) is called.
}
}
Example for By changing the data type.
class ChangeDataType
{
void sum (int a, int b)
{
System.out.println("sum is"+(a+b)) ;
}
void sum (float a, float b)
{
System.out.println("sum is"+(a+b));
}
public static void main (String[] args)
{
ChangeDataType cal = new ChangeDataType();
cal.sum (8,5); //sum(int a, int b) is method is called.
cal.sum (4.6f, 3.8f); //sum(float a, float b) is called.
}
}
{
void sum (int a, int b)
{
System.out.println("sum is"+(a+b)) ;
}
void sum (float a, float b)
{
System.out.println("sum is"+(a+b));
}
public static void main (String[] args)
{
ChangeDataType cal = new ChangeDataType();
cal.sum (8,5); //sum(int a, int b) is method is called.
cal.sum (4.6f, 3.8f); //sum(float a, float b) is called.
}
}
Example for Sequence of Data type of parameters.
class SequnceOfDatatye
{
public void disp(char c, int num)
{
System.out.println("I’m the first definition of method disp");
}
public void disp(int num, char c)
{
System.out.println("I’m the second definition of method disp" );
}
}
class Sample3
{
public static void main(String args[])
{
SequnceOfDatatye obj = new SequnceOfDatatye();
obj.disp('x', 51 );
obj.disp(52, 'y');
}
}
{
public void disp(char c, int num)
{
System.out.println("I’m the first definition of method disp");
}
public void disp(int num, char c)
{
System.out.println("I’m the second definition of method disp" );
}
}
class Sample3
{
public static void main(String args[])
{
SequnceOfDatatye obj = new SequnceOfDatatye();
obj.disp('x', 51 );
obj.disp(52, 'y');
}
}
Constructor Overloading
Whenever same constructor is existing multiple times in the same
class with different number of parameters or order of parameters or type of
parameters is known as Constructor overloading.
The compiler differentiates these constructors by taking into
account the number of parameters in the list and their type.
Constructor overloading can be used to initialize same or
different objects with different values.
Example
// Constructor Overloading
class Box
{
double width, height, depth;
class Box
{
double width, height, depth;
// constructor used when all dimensions
// specified
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
// specified
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions
// specified
Box()
{
width = height = depth = 0;
}
// specified
Box()
{
width = height = depth = 0;
}
// constructor used when cube is created
Box(double len)
{
width = height = depth = len;
}
Box(double len)
{
width = height = depth = len;
}
// compute and return volume
double volume()
{
return width * height * depth;
}
}
double volume()
{
return width * height * depth;
}
}
// Driver code
public class Test
{
public static void main(String args[])
{
// create boxes using the various
// constructors
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
public class Test
{
public static void main(String args[])
{
// create boxes using the various
// constructors
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
double vol;
// get volume of first box
vol = mybox1.volume();
System.out.println(" Volume of mybox1 is " + vol);
vol = mybox1.volume();
System.out.println(" Volume of mybox1 is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println(" Volume of mybox2 is " + vol);
vol = mybox2.volume();
System.out.println(" Volume of mybox2 is " + vol);
// get volume of cube
vol = mycube.volume();
System.out.println(" Volume of mycube is " + vol);
}
}
vol = mycube.volume();
System.out.println(" Volume of mycube is " + vol);
}
}
No comments:
Post a Comment