• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

overriding meth(String... i) wih meth(String[] i)......

 
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This code compiles and the output is....


can anyone explain why the method call with no parameter worked.....
actually the overriden version has a normal array type parameter......
more over how is this a valid override?
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String... is a vararg and String[] are both same.

String... x means many parameters of the same type and accessed by x.

try using public static void main(String... args) and access args[0] from main. Pass an argument and then see if it prints.
 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nitish Bangera wrote:String... is a vararg and String[] are both same.

String... x means many parameters of the same type and accessed by x.

try using public static void main(String... args) and access args[0] from main. Pass an argument and then see if it prints.




sorry i suppose that you didn't understand my question....
all those basics are known to me.............

there are some differences between the two....
(Strings...i) accepts zero arguements whereas (Strings[] ) don't.......

that's why i asked how the overriden method accepted zero arguement call........
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry that i gave in the basic details which you already knew....but according to me new Crap().method(new String[] {}) and new Crap().method() looks same.

Also this can be verified by public static void main(String[] args) which works even if we don supply arguments to it doesn't it. The exception will be thrown if you try to access an array object with like a[0] or a[1].

One more basic thing i like to add is that Array objects declared locally in a method needs an initialization to refer to an object. Here the String[] a is given null which is a value for JVM as you are not passing anything.
 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


if your concept is correct then this also should compile......but unfortunately it doesn't my friend....
main() method is specially designed for starting an application and for command line arguments....

other methods are slightly different.............i suppose...........
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Karthick,

Thanks for the interesting question..

As you would be already aware, the methods method(String...) and method(String[]) are treated internally as same (even overloading is not allowed).
But since the compiler can allow method(String...) to be called as method(), it compiles.
And the call is considered as a over-ride during the run-time as well.

Swapping the methods in the super and sub-classes would give us a fair idea on what's happening. The code doesn't even compile.

The program in fact takes advantage of both the compile-time and run-time polymorphisms.



Regards,
Vinayagar. K
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry

Yeah main is specially designed for this.

Vinayanagar is right.It takes both compile n run time polymorphism. It allows the method as String... can take zero arguements and at run time uses the overridden method even though it doesn't pass an arguement,its not the job of the JVM to check for syntax so it just runs the method and prints the Sub Method. If the parameter was used in the method then there would have been a runtime exception.

Hey great question.Sorry for the vague answers but well i am much clear now. Thanks Vijayanagar
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As already said, the varargs notation with "..." and the old-fashioned notation with an array are essentially the same (but in the case of "...", you could also call the method with no arguments).

There is nothing special with the main method in this regard. The main method is handled exactly the same as any other method, it doesn't behave any different than any other method.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vinayagar Karpagam wrote:Hi Karthick,

Swapping the methods in the super and sub-classes would give us a fair idea on what's happening. The code doesn't even compile.



Vinayagar. K


I compiled and ran the above code. It compiled and ran successfully.The output was Super method;

-Pooja
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. As Pooja mentioned, the code complies. However, this won't compile:
 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jyoti B.Shah wrote:Yes. As Pooja mentioned, the code complies. However, this won't compile:





yeah thank you friend........
here the compiler didn't compile because it sees that the super class doesn't have a method with variable-args parameter....but only with normal array type...
if you are right here compiler would have used compile time polymorphism here as well......

i don't think , there is a concept as compile time polymorphism for non-static,non-private methods......
of course i already knew that references in the same hierarchy can be assigned to each other without any compilation error, but it may throw runtime exception......

sorry for troubling you all, my friends.... i am not very clear in this regard that's why... don't mistake me.....
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if i understood it well how the var-arg works - in compile time it matters what method you have - with var arg or array parameter. Compiler will complain if you try to invoke a method with array parameter in var-arg way. But in runtime all var-args internally are arrays

so these samples of code make sence:







That is how i understand it

-------------------
Best regards,
Anastasia
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic