0

I was asked a question in interview about method overloading.

public class A {

    public void getName(Object o){
        System.out.println("Object method");
    }

    public void getName(String o){
        System.out.println("String method");
    }

}
public class B extends A {

    /**
     * @param args
     */
    public static void main(String[] args) {

        A a = new A();
        a.getName(9.9);
                a.getName(null);
        a.getName();
    }

}

Please provide me the output with explanantion.

singhakash
  • 7,891
  • 6
  • 31
  • 65
user2500313
  • 39
  • 1
  • 2
  • 9
  • You can find the *output* yourself, surely. What have you observed? (Currently, the code won't compile as far as I can see...) – Jon Skeet Mar 30 '15 at 06:45
  • (Then [JLS 15.12.2](http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.12.2) gives the overloading rules.) – Jon Skeet Mar 30 '15 at 06:45
  • for the first two call. i.e a.getName(null); my output is 'String method ' while for a.getName(9.9); my output is 'Object method' and for 'a.getName();' i am getting complie time error. please explain me the same. – user2500313 Mar 30 '15 at 06:46
  • Well what would *you* expect `a.getName()` to do? Why would you expect that to compile? If you're *only* interested in the call with the null argument, it would be better to show just that. What research have you done to try to find out the overloading rules? – Jon Skeet Mar 30 '15 at 06:47
  • you have compile time error because you dint define `getName()` with no parameter.Compiler is saying that he cant find the body of function you are calling because it is not defined – singhakash Mar 30 '15 at 06:48
  • @JonSkeet duplicate only in it's second method, please reopen – S. Pauk Mar 30 '15 at 06:51
  • 1
    @SergeyPauk: Given the title, I strongly suspect the second method is the only one the OP is interested in. It's hard to tell for sure at the moment though. – Jon Skeet Mar 30 '15 at 06:53

0 Answers0