Our social:

Wednesday, October 5, 2016

Method and Constructor Overloading-Java Tutorial

We have already covered the basics of Method and Constructor. Now, we look into some more features of methods and constructor.
In a java class you are allowed to create more than one method with the same name, only constraint you have to take care that the methods must differ in their parameter declaration. This feature is call method overloading.
Now why do you want to create more than one method with the same name? There can be scenario where the functionality of the methods is same, so it is good choice to use the same name for understanding purpose.
But this is ultimately a question of personal choice. There is no rule in java compiler which will stop you to create a method with the name doAddition and inside the method all you do is multiplication instead of addition.
Remember, java compiler only checks the syntax; it does not care about the semantics.
Consider the program below:
Try It
package com.java;

public class MethodOverloadingTest 
{
 public static void main(String[] args) 
 {
  MethodOverloadingTest object = new MethodOverloadingTest();
  
  //call the doAddition method which takes two parameters as input
  int firstSum = object.doAddition(4, 5);
  
  //call the doAddition method which takes three parameters as input
  int secondSum = object.doAddition(14, 20, 7);
  
  System.out.println("The first summation is " + firstSum
    + " and the second summation is " + secondSum);
 }

 public int doAddition(int a, int b) 
 {
  int sum = a + b;
  return sum;
 }

 public int doAddition(int a, int b, int c) 
 {
  int sum = a + b + c;
  return sum;
 }
}
The class has two methods with the same name: doAddition, one is taking two parameters as input, other one taking three inputs. Both are doing same work, just adding the parameters and returning the summation.
Here doAddition method is overloaded in MethodOverloadingTest class.
Pay attention to what we said about the constraint of overloaded methods: 'Methods must differ in their parameter declaration'.

That means the two overloaded method can differ in three sense:

a) The number of parameter can be different. ( As with our example)
b) The number of parameter can be same, but then their data type must be different.
c) The number of parameter and the data type can be same, but in that case the order of parameters must be different.
Violating these rules will give you compilation error.
The number of parameter can be same, but then their data type must be different.
Create a method like below in the previous class:
public float doAddition(float a, int b)
{
 float sum = a + b;
 return sum;
}
This is totally legal, as even though the method taking two parameter, one of the type is float, not int.
The number of parameter and the data type can be same, but in that case the order of parameter must be different.
Try adding this below code into your class
public float doAddition(float a, int b)
{
  float sum = a + b;
  return sum;
}
 
public float doAddition(int b, float a)
{
  float sum = a + b;
  return sum;
}
This is again legal, as even though two methods are taking two parameter and of same type, but their ordering is different.
When you call a method, java compiler checks the parameter you are passing while calling the method. It then decides which version of the overloaded method to execute.
As with our example:
//call the doAddition method which takes two parameters as input
int firstSum = object.doAddition(4, 5);
  
//call the doAddition method which takes three parameters as input
int secondSum = object.doAddition(14, 20, 7);
The comment section describing what the compiler is doing.
One last thing you have to remember about method overloading is the return type of a method do not have any role to play here. That means you are not allowed to write two methods as shown below:
public int doAddition(int a, int b) 
{
 int sum = a + b;
 return sum;
}
 
public void doAddition(int a, int b) 
{
 int sum = a + b; 
}
This will give you compilation error. Even though the return type is different in this two method, they are not overloaded according to the rules, as they does not differ in parameters. As you can guess, this is to help the compiler. For instance suppose java allows you to write a code like shown above, and it is not giving any compilation error. But when you will call doAddition method with two integer parameters, java compiler cannot decide which of the two methods you want to call actually. So java does not allow you to write code like above.

As with method, there can be constructor overloading also. Try to write a code like below 

Try It
package com.java;

public class Shape 
{
 double length;
 double width;

 Shape(double lenght, double width) 
 {
  // this is for rectangle shape
  this.length = lenght;
  this.width = width;

 }

 Shape(double length) 
 {
  // this is for square shape, where length and width are same.
  this.length = length;
  this.width = length;

 }

 double getArea() 
 {
  double area = length * width;
  return area;
 }

 public static void main(String[] args) 
 {
  // initializing the required parameters for rectangle shape
  Shape rectagle = new Shape(3, 4);

  // initializing the required parameter for rectangle shape
  Shape square = new Shape(5);

  System.out.println("The area of rectangle is " + rectagle.getArea()
    + " And the area of square is " + square.getArea());
 }
}
The same rule of method overloading also applies here for the constructor parameters.
Here we have two constructor, one with two parameter and other with one parameter. This kind of design is quite normal in java, as you can see it is giving us flexibility to define two kind of shape in one class. You can create more constructors for as many shape you want. This concept is called constructor-overloading
Click "Try It" button above to execute the program. The above program gives an output as:
The area of rectangle is 12.0 And the area of square is 25.0
Method overloading supports java's Polymorphism feature. Polymorphism means 'many forms'. Same method name is used in different forms. If method overloading was not supported then for each method you had to write different name, even though conceptually it is doing the same job. As with our method overloading example, you can have two method with the same name doAddition, one deals with integer values, other deals with float. May be you can design a third method which deals with a mixture of the two. They are all doing the same job, addition. So using the same name doAdditon is logical here. With the same name you can perform job of same type with different sets of data.

 
           

   Basic Data Types

0 comments:

Post a Comment

Hello Every One thanks For Visit My site You Can Suggest any Idea About site And Related Education Information Provide me Its Best Content For you am Include in this Site And feel free contact And email now I hope This site very help full you