Sunday, April 22, 2018

Method Overloading in java

Method Overloading :
JAVA allows a class to have different methods with the same name but with different signatures. This is called method overloading.



Lets see the other tutorials in this series
  1. Making Games With Java- Introduction
  2. Getting Everything you need for Java
  3. Introduction to Java Programming
  4. Basic data types and Variables in java
  5. Basic Data types and Variables in java - Continued...
  6. User Input using Scanner class in java
  7. Conditional Logic in java
  8. Loops in java
  9. Arrays in java
  10. Wrapper classes in java
  11. Methods in java
  12. Object Oriented Practices
  13. Inheritance in java
Before we understand method overloading in detail, let’s quickly look at the structure of a method in JAVA.
A method consists of the following parts:
  • Method name: The name of the method.
  • Method arguments or input parameters: The parameters that are passed to the method at the time of calling. We can also have methods without an argument.
  • Method body: This includes some operations that the method performs.
  • Return type: This is the type of argument the method returns to the calling module.


Method Overloading is one of the type of Polymorphism in java
These two type of Polymorphism are :-
  • Overloading (Compile time Polymorphism)
  • Overriding (Runtime Polymorphism)
In Java, method overloading is supported only when the methods differ with each other by the number of input parameters that are passed to the method, the type of those input parameters or the sequence of input parameters.
Java does not support operator overloading but there is only one operator overloading is present in Java which is + (it works concat for string and addition with Integer values)
Examples of method overloading:
class Addition {
 int sum(int a, int b) {
  return a + b;
 }
 int sum(int a, int b, int c) {
  return a + b + c;
 }
}
class TestMethodOverloading {
 public static void main(String[] args) {
  Addition a = new Addition();
  System.out.println(a.sum(10, 20));
  System.out.println(a.sum(10, 20, 30));
 }
}
OUTPUT:
30
60
In the above example, Class ‘Addition has two methods named ‘sum’, but they differ with respect to the number of input parameters. The first method takes two integers as input while the second method takes three integers.

Java decides which method to call depending upon the input parameters when the program compiles. 
Hence, method overloading is also called compile-time or static polymorphism. 
Similarly, a class can have different methods with the same name that differ with respect to the type of input parameters. Let’s look at another example:
class Test {
 void message(int a, int b) {
  System.out.println("From method with integer input parameters.")
 }
 void message(String a, String b) {
  System.out.println("From method with String input parameters.")
 }
}
class TestMethodOverloading {
 public static void main(String[] args) {
  Test t = new Test();
  t.message(10, 20);
  t.message("Hello", "java")
 }
}
OUTPUT:
From method with integer input parameters.
From method with String input parameters.
Method overloading also happens when two methods differ by the sequence of data type of parameters.
E.g. :
add(int, double);
add(double, int);
At the time of calling, first method will be called if the first parameter is integer and second parameter is double and the second method will be called if a double value is the first parameter and an integer value is second.
Suppose ‘obj’ is an object of class where these two methods are defined, then:
obj.add(10, 10.5) 
// first method will be called since the first value is integer, second is double.
obj.add(10.5, 10)
// second method will be called since the first value is double, second is integer.
Why Method Overloading is not possible by changing the return type of method only?

Method Overloading is not supported by only changing the return type of the method. Let’s understand it using an example:
class Addition {
 int add(int a, int b) {
  return a + b;
 }
 double add(int a, int b) {
  return a + b;
 }
}
class TestOverloading {
 public static void main(String[] args) {
  Addition a = new Addition();
  System.out.println(a.add(11, 11)); //ambiguity will arise
 }
}
In the above example, class ‘Addition’ has two methods with the same name i.e. ‘add’ and same input parameters, but they both return different data types; first method returns an integer whereas second method returns a double data type.
But, since method overloading happens at compile time, the compiler has no idea which method out of the two should be called. Note that while calling the method there is no reference of the return type and hence the ambiguity arises.
Therefore, this type of overloading is not supported and Java throws a compile time error.

Can we overload java main() method?

Yes, we can overload the main method.
You can have multiple overloaded main methods inside a single class.
Example:
public class Application {
 public static void main(String[] args) {
  System.out.println("main method called by JVM.");
  System.out.println("Output from overloaded main method:" + main(10, 20));
 }
 public static int main(int a, int b) {
  System.out.println("In overloaded main method.");
  return a + b;
 }
}
OUTPUT:
Main method called by JVM
In overloaded main method.
Output from overloaded main method: 30
In the above example, the class application has two main methods (highlighted in yellow), one with a string array as an input parameter and another one with two integers as input parameters.
Note that at the time of execution, JVM will always look for the original main method (with a string array as an input parameter). The overloaded main method can be called explicitly exactly like the other methods in JAVA (highlighted in green in the above example).

Can static methods be overloaded?

Yes, static methods can be overloaded just like the normal methods. Consider the following example:
public class Test {
 static void method1() {
  System.out.println("Test.method1() called.");
 }
 static void method1(int a) {
  System.out.println("Test.method1(int) called.");
 }
 public static void main(String[] args) {
  Test.method1();
  Test.method1(10);
 }
}
OUTPUT:
Test.method1() called.
Test.method1(int) called.




Method Overloading and Type Promotion

JAVA implicitly promotes one data type to another if no matching data type is found at the time of calling the method.
As shown in the above diagram: 

  • The byte datatype can be promoted to short, int, long, float or double. 
  • The short datatype can be promoted to int, long, float or double.
  • The char datatype can be promoted to int, long, float or double.
  • The int datatype can be promoted to long, float or double.
  • The long datatype can be promoted to float or double.
  • And, the float datatype can be promoted to double.
  • Note: JAVA does not de-promote a datatype while calling a method.
Example:
class Addition {
 void sum(int a, long b) {
  System.out.println(a + b);
 }
 void sum(int a, int b, int c) {
  System.out.println(a + b + c);
 }

 public static void main(String args[]) {
  Addition a = new Addition();
  a.sum(20, 20); //now second int literal will be promoted to long  
  a.sum(20, 20, 20);

 }
}
In the above example, type promotion is done while calling the sum (highlighted in yellow above) method.
Note that in the method definition (highlighted in green above), it is taking one int and one long datatype as an input parameter. But, while calling the method, two int parameters are being passed. Hence, java promotes the second int to a long datatype.

Hope you liked this article , Please share it with your colleagues and friends

Thank for reading
Noeik

0 comments:

Post a Comment