• 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

regarding overriding with var agrs

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



output:
====
test

This is only when foo(String... s) . otherwise it will be test1
Here method invocation will be decided at runtime. then how come

a A= new Main();
A.foo("dd");

invokes foo method of 'a' class ?
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe it is not a legal method override. In a legal override, method signature in the subclass should be same as the method signature in the super class. When there is no method overiding, methods of reference variable's class are invoked.

Therefore, when you invoke the method with reference variable 'a' of class 'A', class 'A' 's method is invoked.
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jyoti is correct. The method signature is set at compile time, the method to execute at run time. As the methods will have different signatures (i.e. different arguments) there is no override, just an overload. This is why a.foo is run.

You can see this effect by adding the following to your code
 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One exception:
public void aaa(String...s){}
can be overriden by:
public void aaa(String[] a){}
with warning, but
public void aaa(String[] a){}
can be overriden by:
public void aaa(String...s){}
without any warnings.

 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You sure about that Lukas...? Have you actually tried it? In my understanding your examples will override, but with warnings.

I uses this code as a test
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look please:


The output: ...

I use Eclipse IDE.
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's identical to my "varArg" method and I get a warning.
I've tried yours as well, same same; I get a warning from the command line. I also see it in in Eclipse if I use that.

Have you disabled warnings or something in your IDE?
Have you compiled from the command line?

To be honest, using an IDE to study for the exam is not a good idea. It helps you too much. Use a text editor and get used to doing everything the hard way. How else will you learn the compile/run options?

[There was a mistake in my post - I have corrected it in case I confuse anyone else]
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that's true about overloads. If we aim at overriding, the signature needs to be same. This means the arg list needs to be same too.

Try giving methods with both String... and String[] in the same class. The class won't compile as String... and String[] are virtually the same.
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I have compiled it using:
javac OverridingGotcha.java

No warnings;

And I ran it:
java OverridingGotcha
The output: []

So what do you think?
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct Himanshu, which is why I gave mine different names.
The fact that Arrays and VarArgs are (almost) equivalent would explain the compiler warning.

Lukas, this is what I get when I compile my class On Windows with Java 1.5.0_16:
C:\src>javac Sub.java
Sub.java:3: warning: varArg(java.lang.String[]) in Sub cannot override varArg(ja
va.lang.String...) in Super; overriding method is missing '...'
public void varArg(String[] s)
^
Sub.java:8: warning: Array(java.lang.String...) in Sub cannot override Array(jav
a.lang.String[]) in Super; overridden method has no '...'
public void Array(String... s)
^
2 warnings


If however I use Linux with Java 1.6.0_13, then I get no warnings just like you (but Eclipse still shows a warning). I am going to check that I am not accidentally suppressing warnings somehow....
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The issue is the JVM version and not the IDE or command line.
If you use Java 1.6 compiler then It won't give you warnings however if you use 1.5 version (as with Jason) then it will give warnings.

Cheers!!!
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a way to switch the warnings back on? I can see the "-nowarn" option, but no "-warn" option.

And with 1.5 the message is confusing. It says "cannot" when it should say "should not".
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can compile with -Xlint flag:

javac -Xlint Smth.java

BTW you can use any version of java using -source flag
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code does not compile:



The following code is OK:



With this we can conclude that String... is a superset of String[]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic