• 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

Overloaded Methods in Subclass

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
the following are two ways of coding overloaded methods in super and subclass.
Case 1:

public class A extends Base {
public static void main(String[] args) {
A obj=new A();
char c = 'V';
int i=100;
System.out.println(obj.m1(c));
System.out.println(obj.m1(i));
}
public char m1(long x) {
return 'X';
}
}
class Base {
public char m1(int x) {
return 'Y' ;
}
}
_______________________________________________________________
This gives compile time error of m1 having ambiguous reference to
m1(int) and m1(long)
________________________________________________________________
case 2:

public class A extends Base {
public static void main(String[] args) {
A obj=new A();
char c = 'V';
int u=100;
System.out.println(aww.m1(c));
System.out.println(aww.m1(u));
}
public char m1(long x) {
return 'X';
}
public char m1(int x) {
return 'Z' ;
}
}
class Base {
public char m1(int x) {
return 'Y' ;
}
}
_______________________________________________________________
This compiles ok.
_______________________________________________________________
Could anyone of u tell me the reason of this difference in behaviour.
Thanks
Raj
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Raj,
That's a very good question. I don't know that I know the answer, but here's a theory:
It won't compile when the methods are not both in the A class, right? I'm guessing it has something to do with how much the compiler trusts us. In other words, when we put both the int and long versions in A, the compiler is clear that the int is the one to use (char-->int is closer than char-->long).
But when the long version is in A and the int version is inherited, the compiler wonders whether we know that there is an int version in the Base class and whether we are actually meaning to call it or the long version in the class our object is.
Does that make any sense at all?
Just my thoughts...I'd love to hear what others think.
--liz
------------------
Elizabeth Lester
SCJP Dreamin'
[This message has been edited by Elizabeth Lester (edited September 29, 2001).]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the first example when the compiler looks for the type A for a matching declaration to the "System.out.println(obj.m1(c));" call it finds that both "char m1(long x)" in A and "char m1(int x)" in Base are aplicable. It chooses the most specific of both. But none is more specific than the other so the ambigous error is printed.
The same to "System.out.println(obj.m1(i));"
Now how to determine which method is more specific than the other:
A:char m1(long x)
Base:char m1(int x)
We have to consider both the list of arguments and the class where were declared:
A method is more specific than other if it was declared in a class that is the same or a subclass of the other, and if each of its arguments have the same type or can be converted by a widenning conversion to the corresponding types of the other method.
Is the first method dceclared in A more specific than the one declared in Base? It could be because A extends Base, but it isn't because long can't be converted to int via a widenning conversion.
Is the method declared in Base more specific than the one declared in A? It isn't because Base can't be converted to A via a widenning conversion.
So none is more specific tham the other and the ambigous error is produced.
Read JLS 15.12.2.1 for more on the subject.
Inthe second program applying these rules the method that takes an int argument is more specific than the one that takes a long for both calls.
 
Raj Shah
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jose,
Thanks for that great explanation.
Raj
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic