File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String argument giving two different outputs ?

 
Tanka Banerjee
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,
I have written a program which gives two different ouputs for
two different String Arguments. When String... is passed value is different as compared to String args why ? Please explain me.
Beta.java

class Alpha {
public void foo(String... args)
{ System.out.print("Alpha:foo "); }
public void bar(String a)
{ System.out.print("Alpha:bar "); }
}
public class Beta extends Alpha {
public void foo(String a) {
System.out.print("Beta:foo "); }
public void bar(String a) {
System.out.print("Beta:bar "); }
public static void main(String[] argv) {
Alpha a = new Beta();
Beta b = (Beta)a;
a.foo("test"); b.foo("test");
a.bar("test");b.bar("test");
}
}

Ouput :

E:\>java Beta
Alpha:foo Beta:foo Beta:bar Beta:bar

Beta.java

class Alpha {
public void foo(String args)
{ System.out.print("Alpha:foo "); }
public void bar(String a)
{ System.out.print("Alpha:bar "); }
}
public class Beta extends Alpha {
public void foo(String a) {
System.out.print("Beta:foo "); }
public void bar(String a) {
System.out.print("Beta:bar "); }
public static void main(String[] argv) {
Alpha a = new Beta();
Beta b = (Beta)a;
a.foo("test"); b.foo("test");
a.bar("test");b.bar("test");
}
}

Output:

E:\>java Beta
Beta:foo Beta:foo Beta:bar Beta:bar

 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you expect the output to be identical?
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just because a method is called due to boxing, unboxing, or varargs, doesn't mean that the signature is a match for overriding.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic