*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please explain this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Please explain this" Watch "Please explain this" New topic
Author

Please explain this

Jayakumar Jay
Greenhorn

Joined: Jul 12, 2009
Posts: 8
please explain how this snippet works ,
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

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?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Jayakumar Jay
Greenhorn

Joined: Jul 12, 2009
Posts: 8
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

Joined: Feb 24, 2006
Posts: 6661
    
    5

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.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Jayakumar Jay
Greenhorn

Joined: Jul 12, 2009
Posts: 8
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

Joined: Feb 24, 2006
Posts: 6661
    
    5

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

Joined: Aug 03, 2008
Posts: 9291
    
  17

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 ...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Jayakumar Jay
Greenhorn

Joined: Jul 12, 2009
Posts: 8
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

Joined: Aug 03, 2008
Posts: 9291
    
  17

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

Joined: Jul 12, 2009
Posts: 8
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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please explain this
 
Similar Threads
Please explain how output of this program came?
String argument giving two different outputs ?
Reference Variable
what is the Difference between method overriding and redefining
GC question