Method Overloading Concept Simplified In Java



A method is a name of an action for an object(refers to a String).We refer objects and methods by using their names.We express same word for different meaning. Like

  • open a box
  • open a file
  • open a container
They all express the action open but context changes from line to line.In java the same phenomenon can be replicated and same name with different context is referred as overloading.Methods with the same name but with different parameters (arguments) list are said to be overloaded.The term overloading,as it applies to the method name has more than one meaning.The concept of overloading acme from constructor overloading.Because the constructor name is predetermined by the name of the class so there could be only one constructor.That means we can create an object with only one way.There could be a chance that the object can be created in different ways.So for file opening we can have
  1. default constructor
  2. one argument that takes file name as a String 


class OpenFile{
public OpenFile(){...}//default constructor
public OpenFile(String fileName){...}//argumented constructor
}
 
Here both the constructors as they have the same nae as class name,thus method overloading is essential to allow the same method to be used with different argument types.Although method overloading is must for constructors,it is a general convenience and can be used with any method.

In real time,when we debug an application we use print() method with different data.

Example:

void print(boolean b);//prints boolean value
void print(char c);//prints char value
void print(char[] b);//prints an array of characters
void print(String sb);//prints String value
void print(Object obj);//prints object value
void print(double d);//prints double value
void print(float f);//prints float value
void print(int i);//prints int value
void print(long l);//prints long value
.....
so on...
 
It would be very tedious,inconvenient to have separate identifier for each variant of point.Instead the same name can be used and the methods are overloaded by client of their different parameter lists.The java compiler distinguishes among overloaded methods on the basis of their parameter lists.The method return type is not considered when methods are overloaded.Thus even if there are two different return types,if they have same argument list,they would not be considered as method overloaded.
an Example:

public class OverloadingTest{

     public static void main(String []args){
        int i=0;
        new MyTree(); 
         while(i!=9)
         {
             MyTree t=new MyTree(i);//calling overloaded method
             t.infoAboutTree();
             t.infoAboutTree("rose");
             i++;
         }
     
     }
}
class MyTree{
    int height;
    MyTree(){
         System.out.println("Planting my tree");
         height=0;
    }
    MyTree(int i){
         height=i;
         System.out.println("My tree is growing "+height+" inch tall");
        }
    void infoAboutTree(){
        System.out.println("My tree is growing "+height+" inch tall");
    }
    void infoAboutTree(String myTree){
        System.out.println("My tree"+myTree+" is growing "+height+" inch tall");
    }
}
 
output of the code:
$javac OverloadingTest.java
$java -Xmx128M -Xms16M OverloadingTest
Planting my tree
My tree is growing 0 inch tall
My tree is growing 0 inch tall
My treerose is growing 0 inch tall
My tree is growing 1 inch tall
My tree is growing 1 inch tall
My treerose is growing 1 inch tall
My tree is growing 2 inch tall
My tree is growing 2 inch tall
My treerose is growing 2 inch tall
My tree is growing 3 inch tall
My tree is growing 3 inch tall
My treerose is growing 3 inch tall
My tree is growing 4 inch tall
My tree is growing 4 inch tall
My treerose is growing 4 inch tall
My tree is growing 5 inch tall
My tree is growing 5 inch tall
My treerose is growing 5 inch tall
My tree is growing 6 inch tall
My tree is growing 6 inch tall
My treerose is growing 6 inch tall
My tree is growing 7 inch tall
My tree is growing 7 inch tall
My treerose is growing 7 inch tall
My tree is growing 8 inch tall
My tree is growing 8 inch tall
My treerose is growing 8 inch tall
Why return type is not considered during overloading?

public void calculateArea(int a,int b);
//and 
public int calculateArea(int a,int b);
 
Both these statements will call the same method with different return type.However we can call a method and ignore the return value(referred as calling a method for its side effects since we do not want the return value.Rather we are interested to see other effect of the method call).In this case java will be confused to understand which method -calculateArea(int a,int b) to call.Also these two methods are same in terms of method overloading concept. In this case,compiler will raise a flag stating that the method is already defined.This is why return type is not considered during method overloading.
This phenomenon is often called as polymorphic behavior of methods or polymorphism.
Method Overloading Concept Simplified In Java Method Overloading Concept Simplified In Java Reviewed by Animesh Chatterjee on October 24, 2018 Rating: 5

No comments:

Powered by Blogger.