Win a copy of TDD for a Shopping Website LiveProject this week in the Testing 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

overriding vs. overloading

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I studied the OO concepts regarding method overloading vs. overriding.
Can someone explain why example 1 compiles, by 2 does not?
1)
class A {
int m(int i) { return 1;};
int m(float f) { return 1; } //overloads m(int i)
}
class B extends A {
void a() { m(1); m(5f); } // calls overloaded methods
}
2)
class A {
int m(int i) { return 1;};
}
class B extends A {
int m(float f) { return 1; } // overloads method from super
void a() { m(1); m(5f); }
}
The second example gives me the following error:
interface1.java:45: reference to m is ambiguous, both method m(int) in A and met
hod m(float) in B match
void a() { m(1); m(5f); }
^
1 error
According to Khalid: "A method can be overloaded in the class it is defined in, or in a subclass of its class"
I am sure I followed all the rules for method overloading vs. overriding.
If I change the parameter m(float f) into m(String s) and the calls in class B accordingly, it works fine in both cases.
Any ideas?
Thanks,
Bernd
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might want to have a look at the JLS which contains some paragraphs on that.
15.12.2.2 Choose the Most Specific Method
The paragraph describes exactly the problem you have !
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited November 21, 2001).]
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi bernd,
ineresting question/problem. i never realized that.
regards,
maulin
 
Bernd Stransky
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Valentin,
Thanks for the JSL pointer. I read the section, but it is still confusing to me.
Look at this:
class A { }
class B extends A {
int m(int i) { return 1; }
int m(float f) { return 1; }
void a() { m(22); }
}
That works fine, but the following does not:
class A {
int m(int i) { return 1; }
}
class B extends A {
int m(float f) { return 1; }
void a() { m(42); }
}
Reading JLS (which is a lot of fun to read ;-), it is still not clear to me why moving m(int i) into the superclass creates an ambiguous method invocation.
Maybe your own words can help me.
Sorry, I just started with Java 3 weeks ago.
-Bernd
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bernd Stransky:
the following does not:
class A {
int m(int i) { return 1; }
}
class B extends A {
int m(float f) { return 1; }
void a() { m(42); }
}
Reading JLS (which is a lot of fun to read ;-), it is still not clear to me why moving m(int i) into the superclass creates an ambiguous method invocation.
Maybe your own words can help me.
Sorry, I just started with Java 3 weeks ago.


First you don't have to be sorry, helping other people makes me happy
Then you're right, reading the JLS is much fun ( again)
And absolutely, the JLS, although being very accurate on certain topic may be very vague on others (like this one).
The method lookup is specified as follows:
First the current class is searched for a method that matches exactly the name of the invoked method, the return type, the number AND TYPE of the arguments and then the lookup is processed in the superclass and so on. In this case, in class B no method conforms to that rule since method m in B has a float parameter. But an int is assignable to a float by assignment conversion. Still, the method could be applicable but then when we look at class A we find a method that is applicable too. SO which one to take??? We don't know. That's why there is an ambiguity during the compilation process.
You may read a further paragraph of the JLS that deals with that too. (If you feel like reading it of course
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#45606
Let me know if you still have doubts
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited November 21, 2001).]
 
Bernd Stransky
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
one more time
So this method lookup algorithm works differently when m(int i) and m(float f) are declared in the same class? i.e. the method overloading happens inside one class, instead of superclass-subclass.
Like in :
class A { }
class B extends A {
int m(int i) { return 1;}
int m(float f) { return 1; }
void a() { m(42); }
}
-Bernd
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This indeed is confusing many times, as widening conversions come in to consideration while resolving such call. Have a look at this discussion we had some time ago, and also this article and see if it is of any help.
HTH,
- Manish
[This message has been edited by Manish Hatwalne (edited November 22, 2001).]
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic