Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2008

Tip: Looking for answers? Try searching our database.

Tell me output

Thread view: 
ram - 26 Jan 2008 16:23 GMT
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 .........
Patricia Shanahan - 26 Jan 2008 16:31 GMT
>  public class Test {
>       public void disp(Object o) {
[quoted text clipped - 10 lines]
>
> 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 {
[quoted text clipped - 17 lines]
>
>  why explain .........

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

Patricia
Tom McGlynn - 27 Jan 2008 20:32 GMT
> >  public class Test {
> >       public void disp(Object o) {
[quoted text clipped - 40 lines]
>
> 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
Roedy Green - 28 Jan 2008 00:54 GMT
>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
Signature

Roedy Green, Canadian Mind Products
The Java Glossary, http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.