• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Overloading

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class super1{
void amethod(long l){
System.out.println("long value");
}
}
class sub extends sub4
{
void amethod(int i){
System.out.println("int value");
}
public static void main(String a[])
{new sub3().amethod(10);
}
}
Compile error as reference to amethod is ambiguous.Actually
shouldnt it print int value since the most specific argument
for 10 is selected.
Thanks!
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
super1? sub? sub3? sub4? This can't compile, and I can't tell what it was supposed to be.
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
avn:
Your post does not contain the complete code. Where are classes
sub3, sub4 ?
Nonetheless, your question seems to be similar to one I posed here . I am still a bit confused on that one. Can somebody explain that again?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am sorry .Here is the correct code:
class super1{
void amethod(int i){
System.out.println("int value");
}
}
class sub extends super1
{
void amethod(long i){
System.out.println("long value");
}
public static void main(String a[])
{new sub().amethod(10);
}
}

Error as ambiguous reference to amethod

[This message has been edited by avn (edited August 10, 2000).]
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Following works.. Why...
class super1{
}
class sub1 extends super1
{
void amethod(int i)
{
System.out.println("int value");
}
void amethod(long i)
{
System.out.println("long value");
}
public static void main(String a[])
{
new sub1().amethod(10);
}
}


Originally posted by avn:
i am sorry .Here is the correct code:
class super1{
void amethod(int i){
System.out.println("int value");
}
}
class sub extends super1
{
void amethod(long i){
System.out.println("long value");
}
public static void main(String a[])
{new sub().amethod(10);
}
}

Error as ambiguous reference to amethod

[This message has been edited by avn (edited August 10, 2000).]


 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone pl. clarify my doubt.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This change works:
class super1{
public void amethod(long i){
System.out.println("long value");
}
}
class sub extends super1
{
public void amethod(int i){
System.out.println("int value");
}
public static void main(String a[])
{
new sub().amethod(10);
}
}
as you can see I've changed argument which can be received by overloaded mathods.I suppose this happens because input to overloaded method is not able to change itself to long type.any more suggestions??
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rajesh Gupta:
This change works:
class super1{
public void amethod(long i){
System.out.println("long value");
}
}
class sub extends super1
{
public void amethod(int i){
System.out.println("int value");
}
public static void main(String a[])
{
new sub().amethod(10);
}
}


The above code works coz sub.amethod is choosen to be the Most specific method.
When resolving the method to be invoked, the compiler chooses the most specific method among the maximally specific methods.
A maximally specific method is one that is both - accessible and applicable.
Here both the methods are maximally specific. Coz 10 can be converted to int (by identity conversion) and to long as well (by widening conversion).
When two methods are maximally specific, ONE of them is choosen.
By the rule specified in JLS section 15.12
However if the methods are reversed, the compiler cannot resolve the MOST specific method. Hence the error in the original program.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Original code is :
class super1{
void amethod(int i){
System.out.println("int value");
}
}
class sub extends super1
{
void amethod(long i){
System.out.println("long value");
}
public static void main(String a[])
{new sub().amethod(10);
}
}
fails to compile because of the way u r calling amethod.
if u call a methos like
new sub().amethod(10L); it will compile and work as u want.
now the problem with ur version is there are two amethod() which can accept int parameter the one is amethod(int i) and another is amethod(long i)
so the compiler gets confused.
make any sense ?

 
thomas
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deepak:
Irrespective of in which classes the methods are defined, the one with the integer type parameter IS THE MOST SPECIFIC. Don't you agree? Then why can't the compiler resolve it?
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thomas- you can't ignore the classes the methods are defined in. JLS section 15.12.2.2 specifies the rules for choosing the most specific method, and the class in which a method is defined is part of it. Basically, a method defined in a base class can never be more specific than a method defined in a subclass - even if its argument list is more specific.
If you wish to avoid this sort of situation, then the thing to remember is that when writing a subclass, never overload a method using less specific arguments (e.g. long instead of int) unless you also provide an override using the exact same arguments. If you want, the override can just call the original method using super.methodName():
<code><pre>
class super1{
void amethod(int i){
System.out.println("int value");
}
}

class sub extends super1 {
void amethod(int i){
super.amethod(i);
}
void amethod(long i){
System.out.println("long value");
}
public static void main(String a[]) {
new sub().amethod(10);
}
}
</pre></code>
 
thomas
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jim. Your explanation and your suggestion are very appreciated.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic