Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Default Method Overloading - Doubt

    Hi,
    what is the output of the following code snippet and plese explain the reason for the output.
    class Demo
    {
    void eat(Object o)
    {
    System.out.println("Object");
    }
    void eat(String a)
    {
    System.out.println("String");
    }
    public static void main(String ap[])
    {
    new Demo().eat(null);
    }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by vidya lakshman View Post
    Hi,
    what is the output of the following code snippet and plese explain the reason for the output.
    class Demo
    {
    void eat(Object o)
    {
    System.out.println("Object");
    }
    void eat(String a)
    {
    System.out.println("String");
    }
    public static void main(String ap[])
    {
    new Demo().eat(null);
    }
    }
    Did you give it a try? Were you surprised by the output? Or is this a quiz? If so, what can we win?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    You spent all the time typing that in, and you couldn't click two buttons to compile and run it yourself?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Overloading is a simple concept, I don't want to give too much away, if you take some time and think it's easy to understand. If you want to have one method that takes multiple different types of arguments and still works, you can overload it to take a different arg list.

    for example:
    Java Code:
    public int square(int x){
      return x * x;
    }
    public int square(String x){
      int y = Integer.parseInt(x);
      return y * y;
    }
    This allows you to supply the same method with different arguments and have it still work. If you did not overload it and try passing a String to the first method it will not work correctly since it requires an int.

  5. #5
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    I think it's because of this hierarchy.

    java.lang.Object
    |
    -->java.lang.String

    If you comment out String method, it'll go to Object method.
    If I'm wrong, somebody else correct me.

Similar Threads

  1. Method Overloading Verification
    By lala in forum New To Java
    Replies: 22
    Last Post: 11-30-2010, 05:32 AM
  2. Overloading qn
    By diskhub in forum New To Java
    Replies: 18
    Last Post: 06-07-2010, 05:19 PM
  3. method overloading and arrays.
    By glopez09 in forum New To Java
    Replies: 5
    Last Post: 12-08-2009, 03:02 PM
  4. Replies: 0
    Last Post: 04-17-2008, 07:43 PM
  5. Method/Operator Overloading
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 08:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •