Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overridding or overloading?

 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
modified version of Source:http://www.coderanch.com/t/269249/java-programmer-SCJP/certification/overriding-methods




The above code prints: 1 100 50. Can someone explain to me how b.go(100) and c.go(50) prints 100 and 50? c.go(50) should give a compiler error i thought but it does not??
 
Zaheer Ahmed
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sidhar!

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.
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am confused about this situation
[ June 23, 2008: Message edited by: Ninad Kulkarni ]
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

but why output is 1 100 50 ?

Please help who know that exactly

Regards
Ninad
[ June 23, 2008: Message edited by: Ninad Kulkarni ]
 
Ben Zaidi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Ben
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does not seem to compile for me. Which version of java are you using ?
 
Ben Zaidi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Ben
 
Ben Zaidi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John,

It is not even compiling for me, i am surprised how it is compiling for
them.

Best Regards,
Ben
 
Karl Prenton
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

just look at all the compiler bugs in the release notes...
http://www.eclipse.org/eclipse/development/readme_eclipse_3.3.2.html


eclipse compiler (JDT/ECJ) also gives slightly different warning/error messages.

Using cli or netbeans it seems to compile fine.
[ June 24, 2008: Message edited by: Frank Zito ]
 
Ben Zaidi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Such call for flexibility reasons is converted to varargs in JDK 5.0, means
it is now reading int[] i as varargs at compile time and runtime both so it
will work.

Any other suggestions are welcome.
Ben
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using JDK 6.0.

Such call for flexibility reasons is converted to varargs in JDK 5.0, means
it is now reading int[] i as varargs at compile time and runtime both so it
will work.


Benz i think you are right about reading int[] i as varargs at compile time and runtime. If anyone has other thoughts please share. Thanks.
 
Zaheer Ahmed
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It compiles and runs fine for me but with a warning.

I am using JDK version 1.5 Update 14.
 
Ben Zaidi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use eclipse IDE and you will get an error.

Ben
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but why output is 1 100 50 ?
my doubt is how 100 and 50 prints
I know instance method selected at runtime
and instance variable selected at compile time but
how it happens to output 1 100 50

Regards
Ninad
 
Krishnamoorthy Vuyala Muralidharan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by benz Zaidi:
Use eclipse IDE and you will get an error.

Ben


Its right, i ran the same code which given me a compile-time error. I have Eclipse:
Eclipse Platform

Version: 3.3.2
Build id: M20080221-1800
Java Compiler: 6.0

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.

Hope this is clear.

Best regards
Kris
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply

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

Please reply

Regards
Ninad
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic