• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

String argument giving two different outputs ?

 
Ranch Hand
Posts: 66
  • 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

 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you expect the output to be identical?
 
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!