Tell me output

R

ram

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 .........
 
P

Patricia Shanahan

ram said:
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
 
T

Tom McGlynn

ram said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top