Day23 : Method Overloading

 Method Overloading

The word overloading in object oriented world means multiple version of the same thing. Thus the word Method overloading means providing multiple method in the same scope with the same name.

For Example : 
The two popular method in java called println and print have multiple version and thus are overloaded.

        (1) int a = 10;
              System.out.println(a);
        (2) double b = 1.7;
              System.out.println(b);
        (3) String name = "Sachin";
              System.out.println("Sachin");

But if we're overloading two or more method then these method must compulsory differ from each other in terms of their argument and this difference can be of three types:
  1. Number of Argument can be different.
  2. Datatype of Argument can be different.
  3. Order of Argument can be different.
We must remember that overloading is never possible just on the basis of return type i.e., if two method are having same name and same argument then even if return different datatype still java will give syntax error.

File Name : OvldDemo.java
public class OvldDemo
{
public void show(int a)
{
System.out.println("In Int show "+a);
}
public void show(char a)
{
System.out.println("In Char show "+a);
}
public void show(String a)
{
System.out.println("In String show "+a);
}

}

File Name : UseOvldDemo.java

public class UseOvldDemo
{
public static void main(String[] args) {
OvldDemo obj = new OvldDemo();
obj.show('A');
obj.show("A");
obj.show(65);
obj.show("Hi");
}
}
Output : 
In Char show A
In String show A
In Int show 65
In String show Hi

How Java Handle Overloaded Calls?

Since every overloaded function we have same mean do java handles these calls by looking at argument and it follows three steps:
  1. It searches for exact match i.e., same type of actual and formal argument.
  2. If no exact match is found then java tries two convert the value ( Actual Argument) to the next higher type. Java never converts lower type to higher.
  3. If this also fail then java changes the family. If the actual argument is int, it will convert into float to int.
If all the above way only then Java give syntax error.

Constructor Overloading

Just like we can overload method in java. Similarly we also can load constructor that is in a single class we can have Multiple constructor. The benefit is that it gives flexibility in object initialization. For Example If the name of the class is date then we must provide at-least  constructor to initialize today date as shown below:
        dated = new Date(2,10,2022)
        OR
        Dated = new Date("Out",2,2022)
But when we overload constructor in the class, we must remember that every constructor must appeared that the other constructor in terms of it argument.

Now when the object of the class is created Java will create only one constructor that will choose accordingly. 
                            Exact Math
                            Type Conversion
                            Char Family

Comments

Popular posts from this blog

Day2 : Compiling Java Program with Examples

Day1: Introduction to Java and its feature

What does System.out.printf( "%-15s%03d\n", s1, x) do?