aspose file tools*
The moose likes Beginning Java and the fly likes String argument giving two different outputs ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String argument giving two different outputs ?" Watch "String argument giving two different outputs ?" New topic
Author

String argument giving two different outputs ?

Tanka Banerjee
Ranch Hand

Joined: Aug 16, 2006
Posts: 64
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

Joined: Jul 11, 2001
Posts: 14112
Why would you expect the output to be identical?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18966
    
  40

Just because a method is called due to boxing, unboxing, or varargs, doesn't mean that the signature is a match for overriding.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String argument giving two different outputs ?