File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

VarArgs

 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Why I am getting
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic