aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt var arg Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt var arg" Watch "doubt var arg" New topic
Author

doubt var arg

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707


What I know about var arg is that "var arg argument is converted to array"
eg. int... to int[]

Could anybody please explain the behavior of the above program to me.

Thanks,


cmbhatt
John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332
Also, I get this warning:


Check whether you can call method is made at compile time, based on reference type.

Try to switch those methods, put varargs in Super, and array in Exm1. That works and both print "int..."

This is very interesting overriding.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Yeah Jonh,

The warning we get is quite useful, but I simply ignore that warning, if I am allowed to do so what is harm there except little ambiguity. I did by
exchanging the method argument as you said. It was allowed though with same
kind warning.

By interest I used javap to get what happens of overriding method meth(int...)
I get following:
>javap Exm1




Thanks for your help.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
This looks a bit strange. I get a warning that tells me that the int[] version cannot be overridden by the int... version. But it goes ahead and does it

So is it an error, a warning, or not? Possibly this is a grey area with varargs which is still a matter of discussion (search the java bug database anyone?)

By the way, never ignore warnings unless you completely understand the significance of the warning and test that the code does exactly what you want it to. If you really did not want the overriding behaviour illustrated above then it would be very wrong to ignore the warning.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
camelia codarcea
Ranch Hand

Joined: May 08, 2007
Posts: 36
hello,

could it be possible that in this case the method is not overriden in the child class, but overloaded ?
That is why you cannot call the method from child when the reference variable is of type Super (if it were overriden, you can call only the methods from parent, but the version of the method is decided at runtime; the version from child).

regards,
Camelia


SCJP 5.0 .... 97%
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by camelia codarcea:
hello,

could it be possible that in this case the method is not overriden in the child class, but overloaded ?
That is why you cannot call the method from child when the reference variable is of type Super (if it were overriden, you can call only the methods from parent, but the version of the method is decided at runtime; the version from child).

regards,
Camelia


But the overridden version is called. The variable obj is of type Super and there is only the int[] version of the method available. Super has only that version of the method available. Now, because "int..." is printed, the method must have been overridden by the version in the subclass.
John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332
Originally posted by camelia codarcea:

could it be possible that in this case the method is not overriden in the child class, but overloaded ?


try this one:



output is:


I believe it is overriden, thus polymorphic.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
One question remains although may prove little silly.

If the method is overridden then why doesn't int[] accept int instead of
demanding int[] only.




Thanks,
[ May 11, 2007: Message edited by: Chandra Bhatt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Um... because an int is not an array?
swarna dasa
Ranch Hand

Joined: Mar 15, 2007
Posts: 108
I did run into this issue a while ago, but i was redefining the main method in base class and the subclass.

The concept of varargs must have been since jdk1.1, as the main method takes String[] while we type command line string arguments. (Just that it is exposed to the developers this release )

There is a subtle difference between int[] and int...
int[] needs an array object and does NOT create an array object with the parameters passed.
int... on the other hand creates an array object with the parameters passed, and if you send me an array object, i have less work to do as you did for me
[ May 11, 2007: Message edited by: swarna dasa ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt var arg