In Java it is possible to define two or more methods within the same class which share the same name, as long as their parameter declarations are different. When this is the case, the methods are said to be overloaded and the process is referred to as Method Overloading.
When Java encounters a call to an overloaded method, it simply executes the version of the method whose parameters match the arguments used in the call.
Examples -
Java treats all the below methods as different, even though they are inside the same Class with same method name because of the following reasons -
1. Different number of parameters
2. Differently declared parameters
test( ) // A method without any parameters
{
}
test(int a) //A method with a single integer declared parameter
{
}
test(double d) //A method with a single double declared parameter
{
}
test(int x, int y) //A method with two integer declared parameters
{
}
First method is treated differently from the remaining three methods, as it has no parameters wheres as the second and third methods have single parameter and fourth method has two parameters.
Second method is different from the third methods even though they have same number of parameters, as the second method has integer declared parameter where as third method has double declared parameter. Second method is different from first and fourth as it has single parameter.
Fourth method is different from all the remaining methods, as it has two parameters.
So if we call these methods from a different Class, with different number or different declared parameters the respective method which is exactly matching will be called as explained in the below examples -
1. object.test( ) -> This statement will call the below method
test( ) // A method without any parameters
{
}
2. object.test(5) -> This statement will call the below method as it is passing integer value 5 as argument
{
}
3. object.test(5.5) -> This statement will call the below method as it is passing double value 5.5 as argument
test(double d) //A method with a single double declared parameter
{
}
4. object.test(5, 6) -> This statement will call the below method as it is passing two integer arguments 5 and 6
test(int x, int y) //A method with two integer declared parameters
{
}
Lets implement this concept on Eclipse IDE:
1. Create a new Project 'Fifth Project', add a package named 'fifth_package' and finally add a class named 'OverloadDemo' under the package as shown below:
9. Save and Run 'CallingOverloadMethods' class as Java Application as shown below:
10. Observe that the output is displayed in console of Eclipse IDE as shown below:
Click here to download the project containing 'OverloadDemo' and 'CallingOveraloadMethods' classes used in this post. (You can download this project and import into Eclipse IDE on your machine)
Automatic Type conversions apply to Method Overloading
When an overloaded method is called, Java looks for a match between the arguments used to call the method and the method's parameters. However, this match need not always be exact. In some cases, Java's automatic type conversions can play a role in overload resolution.
For example you have a overload method test(double a), and you are calling test( ) method by passing integer value. What happens?. The calling statement will check if there is any overload method as test(int a), if not it will convert the passing value to double type and pass it to test(double a) overload method.
Lets implement this concept on Eclipse IDE
1. Launch Eclipse IDE, ensure that 'Fifth Project' is available, Create 'Overload' Class as shown below -
2. Create two test methods with the same name inside 'Overload' class where one method is having no parameters where as the other method is having only one double type parameter as shown below and save the 'Overload' class
Click here to download this project containing the 'Overload' and 'Calling' classes used in this post (You can download and import the project into Eclipse IDE on your machine)
Please comment below to feedback or ask questions.
'Constructor Overloading' concept will be explained in the next post.
4 comments:
package arunjava;
public class sample3
{
public static void main(String[] args)
{
Box25 b1=new Box25();
Box25 b2=new Box25();
b1.Dimension(25, 32, 65);
b2.Dimension(25, 45, 62);
System.out.println("volume is"+b1.volume());
System.out.println("volume is"+b2.volume());
b1.Dimension(4, 6, 8);
b2.Dimension(6, 8, 4);
System.out.println("volume is"+b1.vol());
System.out.println("volume is"+b2.vol());
}
}
class Box25
{
double height,width,depth;
int height1,width1,depth1;
public void Dimension(double height, double width, double depth)
{
this.height=height;
this.width=width;
this.depth=depth;
}
public void Dimension(int height1, int width1, int depth1)
{
this.height1=height1;
this.width1=width1;
this.depth1=depth1;
}
double volume()
{
return height*depth*width;
}
int vol()
{
return height1*depth1*width1;
}
}
the result of above programme
volume is0.0
volume is0.0
volume is192
volume is192
i am not able to compute the value of double
hey arun my second doubt is as u said that we can use methods with same name but different parameters but in the above programme i had to use different method name for volume though it has different parameters as double and int
plz do help me out with the above problem thanks in advance
Hello VJ,
I modified your program. now it works proper and displaying proper result.
package demo.com;
public class Sample3
{
public static void main (String[] args)
{
Box25 b1=new Box25();
Box25 b2=new Box25();
b1.Dimension(5, 5, 5);
System.out.println("volume is"+b1.vol());
b2.Dimension(2.0, 2.0, 2.0);
System.out.println("volume is"+b2.volume());
b1.Dimension(4, 6, 8);
b2.Dimension(6.0, 8.0, 4.0);
System.out.println("volume is"+b1.vol());
System.out.println("volume is"+b2.volume());
}
}
class Box25
{
double height,width,depth;
int height1,width1,depth1;
public void Dimension(double height, double width, double depth)
{
this.height=height;
this.width=width;
this.depth=depth;
}
public void Dimension(int height1, int width1, int depth1)
{
this.height1=height1;
this.width1=width1;
this.depth1=depth1;
}
double volume()
{
return height* depth* width;
}
int vol()
{
return height1*depth1*width1;
}
}
package demo.com;
public class Sample3
{
public static void main (String[] args)
{
Box25 b1=new Box25();
Box25 b2=new Box25();
b1.Dimension(5, 5, 5);
System.out.println("volume is"+b1.vol());
b2.Dimension(2.0, 2.0, 2.0);
System.out.println("volume is"+b2.volume());
b1.Dimension(4, 6, 8);
b2.Dimension(6.0, 8.0, 4.0);
System.out.println("volume is"+b1.vol());
System.out.println("volume is"+b2.volume());
}
}
class Box25
{
double height,width,depth;
int height1,width1,depth1;
public void Dimension(double height, double width, double depth)
{
this.height=height;
this.width=width;
this.depth=depth;
}
public void Dimension(int height1, int width1, int depth1)
{
this.height1=height1;
this.width1=width1;
this.depth1=depth1;
}
double volume()
{
return height* depth* width;
}
int vol()
{
return height1*depth1*width1;
}
}
Post a Comment