*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem overloading Super class's method in Sub!? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem overloading Super class Watch "Problem overloading Super class New topic
Author

Problem overloading Super class's method in Sub!?

Captain Cooper
Greenhorn

Joined: Jul 14, 2001
Posts: 4
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

Joined: Jul 09, 2001
Posts: 74
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

Joined: Jul 09, 2001
Posts: 74
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

Joined: Jun 25, 2001
Posts: 13
y it will try to convert long into int


====<BR>FAF
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
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


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Anshul Manisha
Ranch Hand

Joined: Apr 17, 2001
Posts: 74
A similar problem has been discussed here. Might interest you http://www.javaranch.com/ubb/Forum24/HTML/010836.html
regards

AM<BR> <A HREF="mailto:anshulmohan@rediffmail.com" rel="nofollow">anshulmohan@rediffmail.com</A>
Mini Pilla
Ranch Hand

Joined: Jul 15, 2001
Posts: 112
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

Joined: Jul 15, 2001
Posts: 112
I am sorry but I got it.The method is overriding.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem overloading Super class's method in Sub!?