I happened to come across this interesting code:
1 class sample
2 {
3 sample(
String s)
4 {
5 System.out.println("String");
6 }
7 sample(Object o)
8 {
9 System.out.println("Object");
10 }
11 }
12 class constructor
13 {
14 public static void main(String arg[])
15 {
16 sample s1=new sample(null);
17 }
18 }
The answer was: Program compiles correctly and prints "string" when executed
Why is this?
And also, there was another question:
1 class sample
2 {
3 sample(String s)
4 {
5 System.out.println("String");
6 }
7 sample(StringBuffer sb)
8 {
9 System.out.println("StringBuffer");
10 }
11 }
12 class constructor
13 {
14 public static void main(String arg[])
15 {
16 sample s1=new sample(null);
17 }
18 }
And here the answer was: Compile time error as call to constructor at line no. 16 is ambigious.
Why is this?
And can someone please be so kind as to explain these two programs to me?
Thank You