Menu
Topics Index
...
`

Objects1947


If you need explanation Read this topic

If you need Answer Take test on this topic

Objects1947
What will be the output of the following program?
class Visit {
    public void f1(Object o1) {
        System.out.println("Visited Object");
    }
    public void f1(String s) {
        System.out.println("Visited String");
    }
    public static void main(String[] args) {
        new Visit().f1(null);
        new Visit().f1(new Object());
        new Visit().f1(new String());
    }
}

A. Visited Object
Visited Object
Visited String
B. Visited String
Visited Object
Visited Object
C. Visited String
Visited Object
Visited String
D. Some other output
E. Compilation Error or Runtime Error
Topic: Unknown

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

answer is 'C' because when we pass null as a parameter function with String as parameter is called.......

Posted by Sai Teja    2013-11-19 03:45:01


In method overloading when method is invoked with "null" parameter always the method with more type specific is chosen (in this case String is more specific type than Object so method with String parameter is chosen) If there is more than one method which is like more type specific then you can't invoke method with 'null' parameter as it is ambiguous (for instance if we have one more method like
public void f1(Integer s) then null parameter invocation itself is not possible)
So for obvious reasons the answer is C here :)

Posted by Jayachandra Gupta    2013-11-19 12:57:01


Congratulations Sai Teja. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2013-11-20 04:04:28


© meritcampus 2019

All Rights Reserved.

Open In App