The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2000



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


There is no ambiguity at all

Posted by Kishori Sharan on November 04, 2000 at 1:58 PM

If you are getting ambiguity error then you must be using version of java which is lower than 1.3 . Even with java v1.3 you will get ambuguity error only when you make your main ( int f ) in class a, b and c1 non-static ( in b your method is main ( float f ) i.e. your method main in classes will be
class a {
void main( int a ) { }

class b extends a {
void main ( float f ) { }

class c1 extends b {
void main ( int a ) { }

when you write code like
b obj = new c1( ) ;
obj.main ( 3 ) ;
then in this case both methods main ( float f ) in class b as well as main ( int a ) in class a can handle this method call obj.main ( 3 ) because 3 is an int and an int can always be converted in float without loss of precision and hence you are getting this error. But if you call main method as obj.main ( 3f ) ;then you won't get any error because java doesn't try to convert a float to int and there is no ambiguity at all.
The story is different when you define your methods static. When you define your method static then it is known as name-hiding as opposed to inheritance. So when you define your method main as static in class a, b,c then the method call binding is done at compile time as opposed to runtime in case of non-static methods . So when your methods are staic as in your case then the code
b obj = new c1 ( ) ;
obj.main ( 3 ) ; will always call the main method in object b even if at run time obj contains a refernce to an object of class c1. This makes a difference in not getting any ambiguity error when you make all your mathods static.



Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us