Saturday 8 February 2014

Overloading Methods

Methods within a class can have the same name if they have different parameter lists.

For Example
   int sum(int a, int b);
   int sum(int a, int b, int c);

Both methods have the same name sum, and parameters are different.

Overloading methods is differentiated by the number and type of arguments passed to the method.

The return type is not sufficient to differentiate two overload methods

Example
class OverloadEx{
 void print(String s){
  System.out.println("I am String " + s);
 }

 void print(int a){
  System.out.println("I am integer " + a);
 }

 void print(float f){
  System.out.println("I am float " + f);
 }

 public static void main(String args[]){
  OverloadEx obj = new OverloadEx();
  obj.print("HI");
  obj.print(9);
  obj.print(10.01f);
 }
}
   
Output
I am String HI
I am integer 9
I am float 10.01
   
Class OverloadEx, overloaded the method print. 

Some points to remember
1. The return type is not sufficient to differentiate two overloaded methods.
  When you try to differentiate two methods with the return type, then you will get compile time error like “Method is already defined”.
class OverloadEx{
 String print(int s){
  System.out.println("I am integer " + s);
 }
 
 void print(int a){
  System.out.println("I am integer " + a);
 }
}
       
When you try to compile you will get the below error
OverloadEx.java:7: error: method print(int) is already defined in class OverloadEx
        void print(int a){
        ^
1 error

2. By default a decimal number considered to be an integer
class OverloadEx{
 void print(int s){
  System.out.println("I am integer " + s);
 }

 void print(byte a){
  System.out.println("I am byte " + a);
 }

 void print(short a){
  System.out.println("I am short " + a);
 }

 void print(long a){
  System.out.println("I am long " + a);
 }

 public static void main(String args[]){
  OverloadEx obj = new OverloadEx();
  obj.print(10);
 }
}
       
Output
I am integer 10

Above program overloads method print, with 4 parameters byte, short, int and long. Program calls the print() with value 10. By default numbers consider to be primitive. So print with int argument will be called.

3. By default a real value considers as double
class OverloadEx{
 void print(float s){
  System.out.println("I am float " + s);
 }

 void print(double a){
  System.out.println("I am double " + a);
 }

 public static void main(String args[]){
  OverloadEx obj = new OverloadEx();
  obj.print(10.01);
 }
}
       
Output
I am double 10.01
          
4. What is the output of the below program ?      
class OverloadEx{
 void print(float s){
  System.out.println("I am float " + s);
 }

 public static void main(String args[]){
  OverloadEx obj = new OverloadEx();
  obj.print(10.01);
 }
}
       
A real number considers as double by default. So program gives compile time error like below
OverloadEx.java:9: error: method print in class OverloadEx cannot be applied to given types;
obj.print(10.01);
^
required: float
found: double
reason: actual argument double cannot be converted to float by method invocation conversion
1 error
  
5. What is the output of the below program ?             
class OverloadEx{
 void print(float s){
  System.out.println("I am float " + s);
 }

 public static void main(String args[]){
  OverloadEx obj = new OverloadEx();
  obj.print(10.01f);
 }
}

Output       
I am float 10.01
 
          


Constructor varargs                                                 vararg overloading                                                 Home

No comments:

Post a Comment