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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "VarArgs" Watch "VarArgs" New topic
Author

VarArgs

Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Hi, Why I am getting
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

you SHOULD know that for overriding a method, the method arguments SHOULD be same
here you are not overriding the method at all
and about runtime polymorphism, it is applicable only to method overriding

hope this helps

SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
That's right..what I am trying to ask is, At Runtime, I modified the Beta class structure as given below.
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

of course here is runtime polymorphism
the code you have written is confusing yourself only
I modified the code
and see the comments in it

and the runtime polymorphism applies to OVERRIDING methods only
and the method with signature (String a) is not taking part in overriding at all

now you may ask that var-args are chosen at last if the exact method signature is not available
in this case, we have the method with exact signature available
but still the var-args method is chosen due to following reasons
  • Alpha a = new Alpha(); means that reference type is of class Alpha
  • at comile time, there is only one method availabe in class Alpha i.e. var-args method
  • when the compiler sees the call a.foo("test"), it looks for the exact method signature
  • but method public void foo( String a) is not available in class Alpha
  • so at compile time, the method in class Alpha is chosen
  • but as the program runs, due to polymorphism, the overriding method in class Beta is chosen
  • and hence we get the output subclass and not 5
  • Remember that method polymorphism is only for overrding methods and overrding methods have exactly same signature



  • hope this helps
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: VarArgs
     
    Similar Threads
    Reference Variable
    Please explain this
    GC question