Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

overloading inherited method

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have combined the issues discussed in two previous threads into the following code snippet:
class supr {
public void aMethod(int i) {
System.out.println("superclass aMethod(int)");
}
public void bMethod(long l) {
System.out.println("superclass bMethod(long)");
}
}
class sub extends supr {
public void aMethod(long l) {
System.out.println("subclass aMethod(long)");
}
public void bMethod(int i) {
System.out.println("subclass bMethod(int)");
}
static public void main(String[] args) {
sub instanceOfSub1 = new sub();
supr instanceOfSub2 = new sub();
instanceOfSub1.aMethod(1L); // calls subclass method
//will not compile if the argument is int
instanceOfSub2.aMethod(1); // calls superclass method
//will not compile if the argument is long
instanceOfSub1.bMethod(1); // calls subclass method
instanceOfSub1.bMethod(1L); // calls superclass method
instanceOfSub2.bMethod(1); // calls superclass method
instanceOfSub2.bMethod(1L); // calls superclass method
}
}
I find it intriguing that instanceOfSub1.aMethod(1) with integer argument will not compile. The compiler complains that reference to aMethod is ambiguous. I expected the compiler to resolve this by picking the method with the most specific argument type! Why is it not doing that when the method is inherited from a super class?
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The above code is a simplified version of your code containing only
the relevant lines.
Here
aMethod(int) defined in class supr
is
more specific + less specific
aMethod(long) defined in class sub
is
less specific + more specific
Hence both are equally specific i.e. ambiguous.
That means, it is not just the method signature, you also have
to consider the class where it is declared.
Correct me if I am wrong.
[This message has been edited by rajsim (edited July 11, 2000).]
[This message has been edited by rajsim (edited July 12, 2000).]
 
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
rajsim, what do you mean by "less specific + more specific" ?
The aMethod(int i) in the super class is more specific and the aMethod(long l) in the subclass is less specific. Why can't the compiler resolve this?
 
rajsim
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two methods
supr.aMethod(int)
sub.aMethod(long)
Method call amethod(10) originates in sub.
In context of this method call:
Method defined in supr is less specific than a method defined in class sub; as method call is made from within the class sub
But according to argument list, aMethod(int) in class supr is more specific than aMethod(long) in class sub
Hence they are equally specific or ambiguous.
Did I confuse you even more?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic