Wednesday, January 20, 2016

Topic 14: Method Overloading in Java

Overloading Methods


  • In Java it is possible to define two or more methods within the same class that 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

  • Method overloading is one of the ways that Java supports polymorphism

  • When an overloaded method is invoked, Java uses the type and/or number of arguments as its guide to determine which version of the overloaded method to actually call. 

  • Overloaded methods must differ in the type and/or number of their parameters.

  • Overloaded methods may have different return types but the return type alone is insufficient to distinguish two versions of a method.

// The following test method definitions are overloaded
void test()
{ System.out.println("No parameters"); }

void test(int a) // Overload test for one integer parameter.
{
System.out.println("a: " + a);
}

void test(int a, int b) // Overload test for two integer parameters.
{
System.out.println("a and b: " + a + " " + b);
}

double test(double a) // overload test for a double parameter
{
System.out.println("double a: " + a);
return a*a;
}

// Assuming OverloadDemo as class defining the overloaded methods
OverloadDemo ob = new OverloadDemo();

// call all versions of test()
ob.test();
ob.test(10);
ob.test(10, 20);
System.out.println(ob.test(123.25));

  • When an overloaded method is called, Java looks for a match between the arguments used to call the method and the method’s parameters. This match need not always be exact. In some cases, Java’s automatic type conversions can play a role in overload resolution.

// Automatic type conversions apply to overloading.
void test()
{
System.out.println("No parameters");
}


void test(double a)
{
System.out.println("Inside test(double) a: " + a);
}


// Inside main class
OverloadDemo ob = new OverloadDemo();
int i = 88;

ob.test();
ob.test(i); // this will invoke test(double)
ob.test(123.2); // this will invoke test(double)

Java can automatically convert an integer into a double. Therefore, after test(int) is not found, Java elevates i to double and then calls test(double).

Java will employ its automatic type conversions only if no exact match is found.

  • Though practically, each overloaded method can have its own implementation, we should only overload closely related operations.


Overloading Constructors


Like methods, we can also overload constructor methods. Following is the latest version of Box:

class Box
{
double width, height, depth;
// This is the constructor for Box.

Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}

 }

The Box( ) constructor requires three parameters. This means that all declarations of Box objects cannot use default constructor but must pass three arguments to the Box( ) constructor. For example, the following statement is currently invalid: Box ob = new Box();
Solution is to overload the construtor methods.


Using Objects as Parameters


We can pass objects to methods. For ex:

class Test
{
int a, b;

Test(int i, int j)
{
a = i;
b = j;
}

// return true if o is equal to the invoking object
boolean equals(Test o)
{
if(o.a == a && o.b == b)
return true;
else
return false;
}

}  

//Inside main class
Test ob1 = new Test(100, 22), ob2 = new Test(100, 22), ob3 = new Test(-1, -1);

System.out.println("ob1 == ob2: " + ob1.equals(ob2));
System.out.println("ob1 == ob3: " + ob1.equals(ob3));
System.out.println("ob1 == ob1: " + ob1.equals(ob1));//legal. Comparing ob1 to ob1
}
}

One of the most common uses of object parameters involves constructors. We can use existing object to initialize new objects. For ex:

// Notice this constructor. It takes an object of type Box.
Box(Box ob)
{ // pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}


// Inside main class, create boxes using the various constructors
Box mybox1 = new Box(10, 20, 15);
Box myclone = new Box(mybox1); // create copy of mybox1


 

Click for NEXT article.


Please feel free to correct me by commenting your suggestions and feedback.

1 comment: