• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

overloading-overriding question

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone
I was trying to understand how overloading and overriding works when
return type and argument types are objects that are related to each other
Can someone please explain why line number 10 causes a compile error but
line 6 is fine.
The error is line at 17 and says "Reference to out is ambiguous, both
method out(B) in B and method out(A) in C match
If I comment out line 10, the code works fine and results
A.out from B
B.out.
Please explain...
1.class A {
2.public A out (A a) {System.out.println("A.out"); return a;}
3.}
4.class B extends A{
5.public B out (B b) {System.out.println("B.out"); return b;}
6.public A out (A a) {System.out.println("A.out from B"); return a;}
7.}
8.class C extends B{
9.public C out(C c) {System.out.println("main.out"); return c;}
10.public A out (A a) {System.out.println("A.out from C"); return a;}
11.public static void main(String arg[])
12.{
13.C c = new C();
14.A a = new A();
15.B b = new B();
16.c.out(a);
17.c.out(b);
18.}
19.}
 
Author
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First off, for the sake of others reading this, here is a more readable versionof the code:

THIS CODE COMPILES IN THE 1.4.2 RELEASE. In prior releases it generate the folowing compiler error:
A.java:18: reference to out is ambiguous, both method out(B) in B and method out(A) in C m
atch
c.out(b);
^
1 error
This is an example of the old overloaded method matching algorithm that makes absolutely no sense to most Java programmers, nor should it. It is based on a mistake in the original JLS. "out(B) in B" is the most specific method (which is the method chossen in the 1.4.2 release), but "out(A) in C" is found in what is sometimes referred to as the "most specific class". The problem is that overloaded method matching in pre-1.4.2 releases considers the class in which a method is declared (the "declaring class"). It should not. This has been changed. I wrote about this in last week's Java Developer's Journal newsletter at http://www.sys-con.com/story/?storyid=34292, but I would highly recommend reading 5.8 Overloaded Method Matching in Mastering the Fundamentals, Volume 2 (click on link below). I doubt you will encounter examples like this on the exam. Only a handful of people fully understand this subject.
[ Jess added UBB [code] tags to make the code more readable ]
[ October 22, 2003: Message edited by: Jessica Sant ]
 
Doug Dunn
Author
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See also https://coderanch.com/t/243508/java-programmer-SCJP/certification/Overloading
 
I once met a man from Nantucket. He had a tiny ad
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic