Wednesday, 8 February 2017

JAVA Automatic Promotion In Overloading:



Automatic Promotion In Overloading:

While performing overloading method resolution if there is no method with specified argument type compiler wont raise any error immediately.
First compiler promotes that argument to the next level and checked for matched method.If the match is available,then it will be considered and executed.Otherwise,once again promotes that argument to the next level until all possible promotion,still matched method is not available then only compiler raises error.

This promotion of argument type is called automatic promotion in overloading.
byte------->short------------>int-------------->long--------------->float----------------->double
char-------->int------------>long------------>float----------------->double.

Example:
package com.rameshsoft.rameshselenium;

public class AutomaticPromotionDemo {
          public void show(int a){
                   System.out.println("method1");
                   }
                    
                   public void show(int a,int b){
                   System.out.println("method2");
                   }
                    
                    static public void show(float a){
                   System.out.println("method3");
                   }
                   public static void main(String args[]){                    
                             AutomaticPromotionDemo a=new AutomaticPromotionDemo();
                    
                   a.show(88);
                   a.show(9,7);
                   a.show(3.6f);
                    a.show('a');// calling show(int a)---->automatic promotion from char to int
                   }
                  
OUTPUT:
method1
method2
method3
method1

No comments:

Post a Comment