Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please explain this

 
Jayakumar Jay
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please explain how this snippet works ,
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jayakumar,

Please use code tags when posting code here. You can edit your post to add code tags. And have you tried running the code first?
 
Jayakumar Jay
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vijitha, Yes I have run the code,it produces the following output

alpha foo
beta foo
beta bar
beta bar

can you explain....
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has to do with inheritance. If the subclass overrides the method of the parent class, the method called at runtime will depend on the reference and the object that was instantiated.

Is there anything specific that you do not understand ? How do you think the code flow works ?

[EDIT]

Changed inner class to parent class. Boy ! that ll teach me to reply to a post when I am sleepy.
 
Jayakumar Jay
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak Bala wrote:It has to do with inheritance. If the subclass overrides the method of the inner class, the method called on runtime will depend on the reference and the object that was instantiated.

Is there anything specific that you do not understand ? How do you think the code flow works ?


There are no inner classes here.
My question is when I change the variableargs argument in public void foo(String... a) to public void foo(String a) ,the output is

beta foo
beta foo
beta bar
beta bar

Please explain the reason for change in output
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String... amounts to String[] and if you change String... to String, the runtime flow will change based on the new overriding rules.

As a hint, apply the @Override annotation to some of the methods in the derived class. That will give you a good picture of the problem.
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayakumar when you use a reference of type Alpha to call the foo method, then the compiler sees that there's only one foo method in the Alpha class which takes a String... as parameter. So the compiler converts your call of a.foo("test"); to a.foo(new String[] {"test"}); (as you might know, vararg is after all an array). But if you use a reference of type Beta to call the foo method, then the compiler knows that there is a foo method in the Beta class which takes a single String as parameter, so it doesn't touches your code. So this is all the compiler's fault ...
 
Jayakumar Jay
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:Jayakumar when you use a reference of type Alpha to call the foo method, then the compiler sees that there's only one foo method in the Alpha class which takes a String... as parameter. So the compiler converts your call of a.foo("test"); to a.foo(new String[] {"test"}); (as you might know, vararg is after all an array). But if you use a reference of type Beta to call the foo method, then the compiler knows that there is a foo method in the Beta class which takes a single String as parameter, so it doesn't touches your code. So this is all the compiler's fault ...



Yes I agree, that when reference type alpha is used alpha foo method is called, but when I change the variableargs void foo(String... a) to void foo(String a), even though I use same aplha reference, beta foo is called why it is so..
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why it is so


Simple, because of polymorphism, when you change foo(String... a) to foo(String a), then the foo method in Beta class overrides the foo method in Alpha class, that's why the call is polymorphic...
 
Jayakumar Jay
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:
why it is so


Simple, because of polymorphism, when you change foo(String... a) to foo(String a), then the foo method in Beta class overrides the foo method in Alpha class, that's why the call is polymorphic...


Now it is clear..thanks Ankit...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic