Menu Bar

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Saturday, 25 February 2017

What is Method Overloading In Java ?

Method Overloading :

It is also known as Compile time polymorphism or static binding ,because in this case compiler easily guess which method is to call, at compile time. In Method Overloading we have more than one methods with same name but with different arguments, in same class.
There are some rules to achieved method overloading in Java :
  1. All methods must have same name.
  2. All methods must not have same method signature(means no two methods have same type/ same number/ same order of arguments).
  3. Methods return type are not part or rule of method overloading.
  4. Thrown exceptions are not part or rule in method overloading.
public class OverloadingExample {
 
 public void sum(int a,int b){
  System.out.println("METHOD 1 : "+(a+b));
 }
 public void sum(Integer a,Integer b){
  System.out.println("METHOD 2 : "+(a+b));
 }
 public void sum(float a,float b){
  System.out.println("METHOD 3 : "+(a+b));
 }
 public void sum(double a,double b){
  System.out.println("METHOD 4 : "+(a+b));
 }
 public static void main(String[] args) {
  OverloadingExample o =new OverloadingExample();
  o.sum(10,10);
  o.sum(new Integer(20),new Integer(10));
  o.sum(10.32f,10.32f);
  o.sum(10.32,10.32);
 }
}

Output:

METHOD 1 : 20
METHOD 2 : 30
METHOD 3 : 20.64
METHOD 4 : 20.64


      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Share this Blog with yours Friends !!

No comments:

Post a Comment