Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriding var args methods

 
Swapnil Dangore
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,
Have a look at this code

This code is compiling and running with jdk1.5.0_09.
output is "B's method".
How it is possible , because to call B's method ,we should pass an String array to it...
please help...

Regards
swapnil
 
swapnil dangore
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear moderaters,
Before you tell me about Naming policy , I don't know , how my display name is changed to Java Wonderboy....When i tried to update my profile , my display name indeed was swapnil.dangore in the textbox..
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

swapnil

Var-args means 0 to many
In your case you are not calling the method with parameters.
And your are overriding the method which you defined in your A class's

String a[] is same as String... a

If you have any doubt

try to compile the below program you will get compile time error


class A {
public void method(String... arg)
{System.out.println("var-args"); }

public void method(String[] arg)
{System.out.println("Array");
}



}
class TestMain
{
public static void main(String... args)
{
new A().method();
}
}

In this case you are overloading the method.And while you are overloading the mehtod the arguments must change


Thanks
Anil Kumar

[ May 22, 2007: Message edited by: anil kumar ]
[ May 22, 2007: Message edited by: anil kumar ]
 
Sasha Ruehmkorf
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Swapnar.
Don't know if you are aware of this, but your given code does not compile with Java 1.6 It gives "The method method(String[]) in the type B is not applicable for the arguments ()"
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by swapnil dangore:
Dear moderaters,
Before you tell me about Naming policy , I don't know , how my display name is changed to Java Wonderboy....When i tried to update my profile , my display name indeed was swapnil.dangore in the textbox..


Well, it looks like "Java Wonderboy" is a completely different account. If you want to fix that account, you will have to log into that account first. Otherwise, just don't use it.

Henry
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This code does not compile.

The method method(), is being overridden since var args and array are equivalent. Therefore, when you instantiate B, its objects can only see the overridden method that takes an array, for which you need to supply an array as its argument.

valid instantiation would be:
new B().method(new String[]{"test"}); which will print "B's method".
[ May 22, 2007: Message edited by: M Krishnan ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I compile with JDK 1.5.0_11, the code does compile, but with a warning (not error):

This statement is false, however, because the method in B does override the method in A. This is confirmed by the fact that you can execute the code and it prints "B's method".

When I compile the code with JDK 1.6.0_01, it compiles and runs with no warnings or errors.

I assume that Sasha and M Krishnan are using different JDK versions - can you tell us what versions you are using?
[ May 22, 2007: Message edited by: Jim Yingst ]
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From command-line with javac 1.5.0_07, I am getting the same warning.

With eclipse ide (v3.1.2) which uses java.vm.version=1.5.0_11-b03, I am getting the following error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method method(String[]) in the type B1 is not applicable for the arguments ()

at TestMain.main(TestMain.java:18)
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like the problem is only related to Eclipse (3.1). I tried using the same java version (1.5.0_10-b03) in both the command line and Eclipse, and I am getting the warning in both cases but I am able to compile and execute only when using the command line.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for the record, at this point in time I can't think of a single reason that you should study for the SCJP exam using a 1.6 compiler - nothing good can come of it
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic