my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overridding or overloading? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overridding or overloading?" Watch "Overridding or overloading?" New topic
Author

Overridding or overloading?

sridhar row
Ranch Hand

Joined: Jan 16, 2008
Posts: 162
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

Joined: Feb 06, 2007
Posts: 22
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.


Zaheer Ahmed<br />SCJP 5
sridhar row
Ranch Hand

Joined: Jan 16, 2008
Posts: 162
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

Joined: Aug 31, 2007
Posts: 787

I am confused about this situation
[ June 23, 2008: Message edited by: Ninad Kulkarni ]

SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 787

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

Joined: Apr 08, 2007
Posts: 151
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


Ben,
--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)
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Does not seem to compile for me. Which version of java are you using ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Ben Zaidi
Ranch Hand

Joined: Apr 08, 2007
Posts: 151
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

Joined: Apr 08, 2007
Posts: 151
John,

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

Best Regards,
Ben
Karl Prenton
Ranch Hand

Joined: Mar 10, 2008
Posts: 51
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

Joined: Apr 08, 2007
Posts: 151
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

Joined: Jan 16, 2008
Posts: 162
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

Joined: Feb 06, 2007
Posts: 22
It compiles and runs fine for me but with a warning.

I am using JDK version 1.5 Update 14.
Ben Zaidi
Ranch Hand

Joined: Apr 08, 2007
Posts: 151
Use eclipse IDE and you will get an error.

Ben
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 787

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

Joined: Sep 13, 2005
Posts: 52
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

Joined: Aug 31, 2007
Posts: 787

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
 
wood burning stoves
 
subject: Overridding or overloading?
 
Similar Threads
Polymorphism
overriding methods
Codes result and output problem