Method Overloading Demystified

  • 0
Method Overloading is one of the best features of the Object Oriented Language. With Method Overloading, Java implements Polymorphism.

In simple words, overloading means to carry more than the needed. Simply, a method is said to be overloaded if the class has more than one versions of the same function. When, java encounters such method, Java makes use of type and number of parameters of the methods to decide which method is to be taken for a given call.
The main advantage of the method overloading is understood when one studies a non-Object Oriented programming language like C. In C, one needs to remember different names of the functions even if they all did the same task. For example, abs() and fabs() do the same job of calculating the absolute value but programmer needs to learn the name of both the functions because they operate on different input parameters. This complexity has been reduced by Java by implementing polymorphism. In here, the programmer needs to remember a single name for all functions that do the same job but take different inputs. So, cheers to Java !

Java makes use of return type and number of parameters to decide which version to execute, the return type alone is insufficient to distinguish which version is to be executed.

Another important concept related to Java is that java sometimes makes use of automatic type conversion in order to select the version to execute if no exact match is found. The important point to be remembered with this is that the conversion always occurs to higher compatible data type, but not to the lower compatible data type. The given program tries its best to make its point.

//Java Program to understand Method Overloading*/

package ClassIllustration;

class MethodOverloadingDemystified
{
 

 //Volume Method for Cube with Double Edge
 
 double volume(double edge)
 {
  return edge*edge*edge;
 }

 //Volume Method to calculate volume of Cuboid
 
 double volume(double  length,  double breadth, double height)
 {
  return length*breadth*height;
 }

 //Volume Method to calculate volume of Cylinder
 
 double volume(double radius, double height)
 {
  return (3.14*radius*radius*height);
 }

 public static void main(String args[])
 {
  MethodOverloadDemystified mod = new MethodOverloadDemystified();

  System.out.println("Volume of Cube with edge 10 is " + mod.volume(10));
  System.out.println("Volume of Cube with edge 10.00 is " + mod.volume(10.00));
  System.out.println("Volume of the Cuboid with dimensions 12.00, 13.00, 15.25 is " + mod.volume(12.00,13.00,15.25));
  System.out.println("Volume of Cylinder with radius 10 and height 10 is " + mod.volume(10,10));
 }
}


To compile this program
javac -d. MethodOverloadingDemystified.java
And to run
java ClassIllustration.MethodOverloadingDemystified
The output oberseved would be
Volume of Cube with edge 10 is 1000.0
Volume of Cube with edge 10.00 is 1000.0
Volume of the Cuboid with dimensions 12.00, 13.00, 15.25 is 2379.0
Volume of Cylinder with radius 10 and height 10 is 3140.0
Now, as we have seen there is not method of volume(int). The first call works because in here 10 has been promoted to double and then first definition matches the call, so, this version is executed.

No comments:

Post a Comment