When two or more methods within the same class and have the same method name but different data type or a number of arguments are said to be Method overloading.

Why method overloading calls compile-time polymorphism?
In simple words, method calls are resolved at compile time in case of method overloading. That is why it is also known Compile time Polymorphism

Why method overloading is used?
Method overloading is mainly used in a situation when we want to create multiple different functionalities of a method in a class with the same method name.

Method Overloading Automatic Data Type Conversion.

Tricky Method Overloading Interview Question in java?

public class MethodOverloading {

 public static void overloadingMethod(Object obj) {
 System.out.println("MethodOverloading with parameter type - Object");
 }

 public static void overloadingMethod(String str) {
 System.out.println("MethodOverloading with parameter type - String");
 }

 public static void overloadingMethod(StringBuffer strBuf) {
 System.out.println("MethodOverloading with parameter type - StringBuffer");
 }

   public static void main(String[] args) {
         overloadingMethod(null);
 }
}

OUTPUT: It will give Compiler Time error.

Using (null) does not affect your code but the compiler can't finish compiling because it does not know which one to use.
If you see Object, String and StringBuffer all can be null it is hard to find by the compiler which method it invokes that's the reason it creates ambiguity error.


Tech Mahindra Interview
Method Overloading


public class MethodOverloading {

 public static void overloadingMethod(Integer num) {
  System.out.println("MethodOverloading with parameter type - Integer");
 }

 public static void overloadingMethod(Number num) {
  System.out.println("MethodOverloading with parameter type - Number");
 }

 public static void main(String[] args) {
  overloadingMethod(null);
 }
}

OUTPUT: MethodOverloading with parameter type - Integer

It will choose Integer first coz of the deepest class as you can see in this Hierarchy.

public class MethodOverloading {

 public static void overloadingMethod(Integer num) {
  System.out.println("MethodOverloading with parameter type - Integer");
 }

 public static void overloadingMethod(Long num) {
  System.out.println("MethodOverloading with parameter type - Long");
 }

 public static void main(String[] args) {
  overloadingMethod(null);
 }
}


OUTPUT: It will give Compiler Time error.

If you see, Integer and Long can be null it is hard to find by the compiler which method it invokes and it creates ambiguity error.

public class MethodOverloading {

 public static void overloadingMethod(Integer num) {
  System.out.println("MethodOverloading with parameter type - Integer");
 }

 public static void overloadingMethod(Long num) {
  System.out.println("MethodOverloading with parameter type - Long");
 }

 public static void main(String[] args) {
  overloadingMethod(1);
 }
}

OUTPUT: MethodOverloading with parameter type - Integer.
It will choose Integer first coz of the deepest class.

public class MethodOverloading {

 public static void overloadingMethod(double num,int num2) {
  System.out.println("MethodOverloading with parameter double and int");
 }

 public static void overloadingMethod(int num2,double num) {
  System.out.println("MethodOverloading with parameter int and double");
 }

 public static void main(String[] args) {
  overloadingMethod(1.1d,5);
  overloadingMethod(5,5.5);
  //overloadingMethod(5,5);
 //Compile Time Error--> The method overloadingMethod(double, int) is ambiguous for the type MethodOverloading
 }
}

OUTPUT:
MethodOverloading with parameter double and int
MethodOverloading with parameter int and double


In the above case if you give proper Argument it will compile and run.

public class MethodOverloading {

 public static void overloadingMethod(long num,int num2) {
  System.out.println("MethodOverloading with parameter long and int");
 }

 public static void overloadingMethod(int num2,long num) {
  System.out.println("MethodOverloading with parameter int and long");
 }

 public static void main(String[] args) {
  overloadingMethod(1l,5);
  overloadingMethod(5,5l);
 //overloadingMethod(5,5);
 //Compile Time Error--> The method overloadingMethod(long, int) is ambiguous for the type MethodOverloading
 }
}


OUTPUT:
MethodOverloading with parameter long and int
MethodOverloading with parameter int and long


In the above case if you give proper Argument it will compile and run.

public class MethodOverloading2 {
 
 public static void overloadingMethod(Object type) {
  System.out.println("MethodOverloading with parameter type - Object");
 }

 public static void overloadingMethod(String type) {
  System.out.println("MethodOverloading with parameter type - String");
 }

 public static void main(String[] args) {
  overloadingMethod("Test Method Overloading");
  overloadingMethod(null);
  overloadingMethod((Object)null);

 }
}

OUTPUT:
MethodOverloading with parameter type - String
MethodOverloading with parameter type - String
MethodOverloading with parameter type - Object


It will choose String first coz of the deepest class as you can see in this Hierarchy.


What if we Change return Type of Method Overloading?
public class MethodOverloading {
 
 public static Integer  overloadingMethod(int type) {
  System.out.println("MethodOverloading with parameter return type - int");
  return type;
 }

 public static Float overloadingMethod(float type) {
  System.out.println("MethodOverloading with parameter return type - float");
  return type;
 }

 public static void main(String[] args) {

  overloadingMethod(1);
  overloadingMethod(1.5f);

 }
}

OUTPUT:
MethodOverloading with parameter return type - int
MethodOverloading with parameter return type - float


First of all Return type of method is never part of method overloading, so only changing the return type of method does not affect to method overloading concept.

What happen if Method Overloading with Throws Exceptions?

public class MethodOverloading {
 
 public static void  overloadingMethod(int type) throws NumberFormatException{
  System.out.println("MethodOverloading with parameter type - int");
 }

 public static void overloadingMethod(float type) throws NullPointerException {
  System.out.println("MethodOverloading with parameter type - float");
 }
 
 
 public static void main(String[] args) {

  overloadingMethod(1);
  overloadingMethod(1.5f);

 }
}

OUTPUT: MethodOverloading with parameter return type - int
MethodOverloading with parameter return type - float


Thrown Exceptions from methods are not going to affect Method Overloading. So your overloaded method can throw the same exception or a different exception or it simply does no throws an Exception, no effect at all on method loading.


In Method Overloading one overloaded method as static and another one non-static?
Yes. Overloaded methods can be either static or non-static no issue.

In case of Method Overloading can make overloaded methods be synchronized?
Yes. Overloaded methods can be synchronized.

In Method Overloading can make overloaded methods as final?
Yes, we can declare overloaded methods as final.

Method Overloading Automatic Data Type Conversion.

Post a Comment

Previous Post Next Post