What is method ambiguity in java







   

 This  is  simple  example   of     how    java  methods   can  be ambiguous  and   how  we can  

avoid  it .  And  how  JVM  decide  which  method   to select.



package Java8;

/**  @auther  Mukesh   Jha 
  *
 */

public class Overloading {
     
public static void main(String[] args) {


// TODO Auto-generated method stub
//m1( null);    Method  m1()  is  ambiguous  for  this  this value.
   
  OverloadingA.m1(17);
OverloadingB.test( 10) ;


  // OverloadC.test(null);      Method  test  is ambiguous  Due to  test(Integer g)
OverloadC.test("String");
 
}

}



   class   OverloadingA{
Integer i=null; //
public  static void  m1( String s) {
 
  System.out.println( s);
 
  }
  public  static  void  m1( Integer  s) {
 
  System.out.println( s);
 
  }
}
   
class    OverloadingB{
public  static void test(long lng) {
  System.out.println("Long");
   
}
 
  public static  void test(Integer integer) {
  System.out.println("Integer");
}
}


class    OverloadC {
public static void test(String str) {
 
    System.out.println("String");
}
public  static void test(Object obj) {
 
        System.out.println("Object");
   
      }
public static  void test(Integer integer) {
  System.out.println("Integer");
}

}



What is method ambiguity in java What  is   method  ambiguity  in java Reviewed by Mukesh Jha on 10:37 AM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.