Tell me output

Discussion in 'Java' started by ram, Jan 26, 2008.

  1. ram

    ram Guest

    public class Test {
    public void disp(Object o) {
    System.out.println("Object Vesion");
    }
    public void disp(String s) {
    System.out.println("String Vesion");
    }
    public static void main(String args[]) {
    Test t=new Test();
    t.disp(null);
    }
    }

    the above compiles and displays String Vesion.how it is....explain....


    public class Test {
    public void disp(StringBuffert sb {
    System.out.println("StringBuffer Vesion");
    }
    public void disp(String s) {
    System.out.println("String Vesion");
    }
    public static void main(String args[]) {
    Test t=new Test();
    t.disp(null);
    }
    }

    the aboce code generates comile time error
    Test.java:10: reference to disp is ambiguous, both method
    disp(java.lang.StringB
    uffer) in Test and method disp(java.lang.String) in Test match
    t.disp(null);
    ^

    why explain .........
    ram, Jan 26, 2008
    #1
    1. Advertising

  2. ram wrote:
    > public class Test {
    > public void disp(Object o) {
    > System.out.println("Object Vesion");
    > }
    > public void disp(String s) {
    > System.out.println("String Vesion");
    > }
    > public static void main(String args[]) {
    > Test t=new Test();
    > t.disp(null);
    > }
    > }
    >
    > the above compiles and displays String Vesion.how it is....explain....


    Object is a superclass of String, so although both versions are
    applicable, the String version is more specific.

    >
    >
    > public class Test {
    > public void disp(StringBuffert sb {
    > System.out.println("StringBuffer Vesion");
    > }
    > public void disp(String s) {
    > System.out.println("String Vesion");
    > }
    > public static void main(String args[]) {
    > Test t=new Test();
    > t.disp(null);
    > }
    > }
    >
    > the aboce code generates comile time error
    > Test.java:10: reference to disp is ambiguous, both method
    > disp(java.lang.StringB
    > uffer) in Test and method disp(java.lang.String) in Test match
    > t.disp(null);
    > ^
    >
    > why explain .........


    There is no superclass/subclass relationship between String and
    StringBuffer, so neither version is more specific than the other.

    Patricia
    Patricia Shanahan, Jan 26, 2008
    #2
    1. Advertising

  3. ram

    Tom McGlynn Guest

    On Jan 26, 11:31 am, Patricia Shanahan <> wrote:
    > ram wrote:
    > > public class Test {
    > > public void disp(Object o) {
    > > System.out.println("Object Vesion");
    > > }
    > > public void disp(String s) {
    > > System.out.println("String Vesion");
    > > }
    > > public static void main(String args[]) {
    > > Test t=new Test();
    > > t.disp(null);
    > > }
    > > }

    >
    > > the above compiles and displays String Vesion.how it is....explain....

    >
    > Object is a superclass of String, so although both versions are
    > applicable, the String version is more specific.
    >
    >
    >
    >
    >
    > > public class Test {
    > > public void disp(StringBuffert sb {
    > > System.out.println("StringBuffer Vesion");
    > > }
    > > public void disp(String s) {
    > > System.out.println("String Vesion");
    > > }
    > > public static void main(String args[]) {
    > > Test t=new Test();
    > > t.disp(null);
    > > }
    > > }

    >
    > > the aboce code generates comile time error
    > > Test.java:10: reference to disp is ambiguous, both method
    > > disp(java.lang.StringB
    > > uffer) in Test and method disp(java.lang.String) in Test match
    > > t.disp(null);
    > > ^

    >
    > > why explain .........

    >
    > There is no superclass/subclass relationship between String and
    > StringBuffer, so neither version is more specific than the other.
    >
    > Patricia


    For a little more discussion of this you might take a look at #46 in
    Bloch and Gafter's Java Puzzlers. This is in any case a fun book to
    read and illuminates many dark nooks and crannies in Java.

    Regards,
    Tom McGlynn
    Tom McGlynn, Jan 27, 2008
    #3
  4. ram

    Roedy Green Guest

    On Sat, 26 Jan 2008 08:23:38 -0800 (PST), ram <>
    wrote, quoted or indirectly quoted someone who said :

    >the aboce code generates comile time error
    > Test.java:10: reference to disp is ambiguous, both method
    >disp(java.lang.StringB
    >uffer) in Test and method disp(java.lang.String) in Test match
    > t.disp(null);
    > ^


    see
    http://mindprod.com/jgloss/compileerrormessages.html#REFERENCEAMBIGUOUS
    --
    Roedy Green, Canadian Mind Products
    The Java Glossary, http://mindprod.com
    Roedy Green, Jan 28, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Bentley

    Output / Debug window output bug?

    John Bentley, Sep 10, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    576
    John Bentley
    Sep 10, 2003
  2. yogesh
    Replies:
    1
    Views:
    349
    Victor Bazarov
    Mar 14, 2007
  3. jaffarkazi
    Replies:
    10
    Views:
    394
    Richard Bos
    Jul 4, 2008
  4. David Landrith
    Replies:
    0
    Views:
    106
    David Landrith
    Jul 29, 2003
  5. Mike A
    Replies:
    17
    Views:
    242
    Dr John Stockton
    Nov 19, 2003
Loading...

Share This Page