wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overriding var args methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overriding var args methods" Watch "overriding var args methods" New topic
Author

overriding var args methods

Swapnil Dangore
Greenhorn

Joined: Apr 14, 2006
Posts: 1
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


Cheers<br />Swapnil<br />SCJP 81%<br /> <br />"Dictionary is the only place where Success come before Work"
swapnil dangore
Ranch Hand

Joined: Jun 05, 2006
Posts: 46
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..


Cheers<br />-------------<br />Swapnil<br /> <br />SCJP5-81%<br /> <br />"Dictionary is the only place where Success come before Work"
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
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

Joined: Mar 29, 2007
Posts: 115
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
Sheriff

Joined: Sep 28, 2004
Posts: 18507
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178


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

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
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

Joined: Mar 22, 2007
Posts: 329

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.


SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8801
    
    5
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


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: overriding var args methods
 
Similar Threads
accessability of outerclass members in inner class !!!
A Gud Question on Inner class
static, instance, constructor call order
About Overridden methods...
overriding vs hiding