Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem overloading Super class's method in Sub!?

 
Captain Cooper
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How come the Version 1 works But Version 2 does not?
VERSION 1
<code>
class Base {
void amethod(long l) {
System.out.println("Base long");
}
}
public class Scope extends Base {
public static void main(String argv[]){
Scope s = new Scope();
s.amethod(1);
}
void amethod(int i) {
System.out.println("Scope int");
}
}
</code>
VERSION 2
<code>
just twist the two signatures of amethod(...) and it does not compiles for a method call from main() like s.amethod(1); However, in that case if you put s.amethod(1L) that works!
</code>
my point is why is it not necessary to specify 1L in the 1st version?
NB: I'm using jdk1.3

[This message has been edited by Captain Cooper (edited July 15, 2001).]
 
Eric Pramono
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
When I try to compile the 2nd version (using Jdk1.3) I get this error:
Scope.java:9: reference to amethod is ambigious, both method amethod(int) in Base and method amethod(long) in Scope match
s.amethod(1);
^
1 error
There's a good reference in JLS 15.12.
But, it's a bit hard for me to understand, too.
Maybe someone can help me figure out in "English"
- eric

 
Eric Pramono
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think I got it...
JLS 15.12.2.2 Choose the Most Specific Method
quote:
================================================================
... The Java programming language uses the rule that the most specific method is chosen ...
Let m be a name and suppose that there are two declarations of methods named m, each having n parameters. Suppose that one declaration appears within a class or interface T and the types of the parameters are T1, ..., Tn; suppose moreover than the other declaration appears within a class or interface U and that the types of the parameters are U1, ..., Un. Then the method m declared in T is more specific than the method m declared in U if and only if both of the following are true:
a) T can be converted to U by method invocation conversion
b) Tj can be converted to Uj by method invocation conversion, for all j from 1 to n.
================================================================
so, for the 1st version:
T = Scope
Tj = int
U = Base
Uj = long
method invocation = m(int)
so, the method m declared in T is more specific than the method m declared in U, 'cause there's no problem in converting int to long.
but, for the 2nd version:
T = Scope
Tj = long
U = Base
Uj = int
method invocation = m(long)
there'll be a method invocation convertion problem (possible loss of precision) when converting long to int (rule b)
correct me if i'm wrong..
- eric
 
Faisal Farooqui
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
y it will try to convert long into int
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eric,
Think you got it right. In version 2, for the moment you are passing '1' which can legally be either a 'long' or an 'int' but the compiler doesn't know if, at some later point, you might use a value that won't fit into an 'int' ... it doesn't know wether it should write the bytecode using 'amethod(long)' or 'amethod(int)' so it stops and gives you an 'ambiguous' error message.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Anshul Manisha
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A similar problem has been discussed here. Might interest you http://www.javaranch.com/ubb/Forum24/HTML/010836.html
regards
 
Mini Pilla
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I am sorry but I am dragging but don't u think the output should be double in base class as that is overloaded ???

class One{
void test(double d){
System.out.println( d);
System.out.println("double in base class");
}
}
public class One1 extends One{

void test(double d){
System.out.println( d);
System.out.println("double in sub class");
}
void test(int i){
System.out.println(i);
System.out.println("int in sub class");

}
public static void main(String[] args){
One t = new One1();
int i=128;
t.test(i);
}
}
Thanx
 
Mini Pilla
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry but I got it.The method is overriding.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic