Method Overloading - What is the output of following code snippet ?

Code Snippet 1 :

public class Test {

        public static void main(String[] args) {               
                Test t = new Test();
                t.method1(null);        
        }
       
        public void method1(String s)
        {
                System.out.println("Print String");
        }
       
        public void method1(Object k)
        {
                System.out.println("Print Object");
        }
}

Output :

Print String

Code Snippet 2 :

package com;

public class Test {

        public static void main(String[] args) {               
                Test t = new Test();
                t.method1(null);        
        }    
       
        public void method1(String s)
        {
                System.out.println("Print String");
        }
       
        public void method1(Object k)
        {
                System.out.println("Print Object");
        }
       
        public void method1(Test k)
        {
                System.out.println("Print Object");
        }      
}

Output :

Compilation Error!!! The Method method1 is ambiguous for the type Test.
Line number : 4
Interview Questions: 

Search