I tried this example and it ran fine but with this warning:
First thing is that you get this warning because compiler actually finds an equivalent of go(int d) in the parent class, but its not exactly the same that is taking an array as argument. But the compiler knows that the superclass's go method can handle the arrays if it gets so it compiles fine.
At runtime the methods that are actually executed are based upon the underlying object of the reference variable.
a.go runs the A class's go() method because a refers to an object which is of type A. b.go runs the B class's go() method because b refers to an object which is of type B. Same is the case for c.
Hope this helps.
Zaheer Ahmed<br />SCJP 5
Joined: Jan 16, 2008
Thanks for the reply Zaheer. But when there is no Parent class reference why will the compiler check for it? For example:
And what do you make of the warning you are getting?
Main.java:8: warning: go(int) in B cannot override go(int...) in A; overriding method is missing '...'
If B cannot override go(int...) in A how is the polymorphic call happening?
It compiles fine printing same output as mention above and I am not getting warning how this process happens at runtime I know instance method selected at runtime and instance variable selected at compile time
Same situation is giving me a compile time error. When third statement is encountered, it says c.go() is not applicable for type int arguments. It weird because int...number and int number is a same thing. We just over-rided this thing, how it is compiling fine on your machines.
--Lost in preparation of SCJP and SCWCD--
"Start writing a new chapter, for if you live by the book you'll never make history." (Ben Sobel)
It is giving me a compile time error as expected. Now i remember there was one bug something which is happening here in jdk1.5 which was fixed in later releases, can you guys tell me what version you guys are exactly using?
Joined: Apr 08, 2007
It is not even compiling for me, i am surprised how it is compiling for them.
I get an compiler error - but only in eclipse ide (v3.3.2). I think this is an example of why we seriously shouldn't use the eclipse ide! It uses it's own compiler - which appears to have a bug wrt varargs?!? @mods please confirm this?
After I changed the Java compiler version to 5.0 still it didnt work! I have to yet try to run the same without an IDE.
However, as far as my understanding varargs and arrays are treated as very same by the compiler. That means in the above code, the subclass B inherits the method go(int... i) which the argument has been changed from varargs to array. It should not be a problem, said like this, c.go(100) will invoke the method on the subclass as the runtime object is subclass B and not superclass A. So at the runtime the subclass version of the method go(int i) will be invoked returning the first element of the array or vararg. Therefore it results in 1,100,50 instead of 1,1,1.
when you interchange overriden with overriding method declaration in both super and sub classes then compiler will not allow to go(100) with go(int) compiler only expect int to method and when we pass array then compiler accept it
but in this program go(int...) is overridden with go(int) overriding method perfectly reverse is also true for overriding
Let consider following situation where we not implement overriding and only one class not super class and sub class
we can pass go(int) to go(int...) in case of we not override anything it is ok whether we pass int or int to method it compiles and runs fine
but in case we pass go(int) to go(int) then we got compiler error