aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes help! I can't understand. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "help! I can Watch "help! I can New topic
Author

help! I can't understand.

jack zhu
Greenhorn

Joined: Jul 10, 2001
Posts: 6
why the output is 128.0 not 128 ? I remeber that: the variable that is invoked depends on the type of the Object and the method that is invoked depends on the Object itself. cos' the method of the superclass is not be overrideen?
class Sub extends Super {
public static void main(String [] args) {
Super sup = new Sub();
int i=128;
System.out.println(sup.test(i));
}
int test(int i) { return i; }
}
class Super {
double test(double d) { return d; }
}
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
That is because double variables have decimal precision. Eventhough you are passing in an int to test() method, it is being received as a double. Int to double conversion is automatic.
The opposite is also true. Any number that has a decimal point in it is treated as double by default.
Hope that helps!
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Jack,
The way the JVM finds out which method to call is that it starts at the lowest heirarchy level and works it way up. It does this with overridden methods only. Since you have invoked Super.test method with a signature of double (ret) and double (param), it finds no match in the Sub class because you have not overridden the Super function that you have called. In order for you to call your local test method you would need to do one of two things:
1. Override the double test( double ) method in class Sub
or
2. Use the following line in your main method:
System.out.println( ((Sub)sup).test( i ) );
Regards,
Manfred.
Kaspar Dahlqvist
Ranch Hand

Joined: Jun 18, 2001
Posts: 128
Aahh!
Noooow I get it! Thanks Manfred! You rule!
//Kaspar
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
Manfred, wouldn't a simple call to "test(i)" invoke the method in the subclass?
Anshul Manisha
Ranch Hand

Joined: Apr 17, 2001
Posts: 74
A simple call to test(i) won't work because the method test is not static.
Originally posted by Scott Appleton:
Manfred, wouldn't a simple call to "test(i)" invoke the method in the subclass?

Some interesting observations
I changed the code as follows

I got the output as 128.0
however if I have following code

I get the answer as 128 ( no decimal) Using JDK 1.2
In first case i is passed as double and in second case i is passed as int(as it should be). I had an earlier situation with similar code. I was trying to compile code under JVM 1.3 and it gave me a compiler error. Please checkout following discussion http://www.javaranch.com/ubb/Forum24/HTML/010439.html
If you take the method with int signature to superclass and the method with double signature to subclass you get 128.
If you change the type of t as follows
TryOne1 t = new TryOne1();
and then try to compile the following code

you get a compile time error in JDK 1.2

TryOne1.java:15: Reference to test is ambiguous. It is defined in void test(doub
le) and void test(int).
t.test(i);

However if you define both methods in one class it does not complain...
Interesting ain't it?

[This message has been edited by Anshul Manisha (edited July 11, 2001).]


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

Joined: Apr 17, 2001
Posts: 74
Also if you have code like this

then you would get the answer as 128 and not 128.0
There is something fishy going on here which I am unable to put my finger at.
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Anshul,
In your case, the method int test(int i) happens to be the most specific method that is accessible and hence it gets called. It is a simple case of method overloading.
If the method resolution results in an ambiguity because the compiler finds more than one applicable and accessible method, it generates an error.
Hope that helps!
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.
[This message has been edited by Ajith Kallambella (edited July 11, 2001).]
Anshul Manisha
Ranch Hand

Joined: Apr 17, 2001
Posts: 74
I understand that Ajith but if method int test(int i) is in subclass then suddenly it is not the most specific method (see original post by Zack) my question is why overloaded method is behaving differently when we overload it in subclass vs. when we overload it in superclass. If you look at my second post and zacks first post they are similar classes only difference being methods being declared at a different place(i.e. in class Super)
regards,
Originally posted by Ajith Kallambella:
Anshul,
In your case, the method [b]int test(int i)
happens to be the most specific method that is accessible and hence it gets called. It is a simple case of method overloading.
If the method resolution results in an ambiguity because the compiler finds more than one applicable and accessible method, it generates an error.
Hope that helps!
[/B]


[This message has been edited by Anshul Manisha (edited July 11, 2001).]
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
I have to admit this one is very non-intuitive to me. It's as if the compiler somehow gives less credibility to a method which is inherited versus one which is explicitly declared in the subclass, to the point where an explicitly-declared but less precise method is given equal weight to a more precise but inherited method.
If so, I don't see why Java is programmed that way. Why force us to explicitly override a method in a subclass in order to compile cleanly? (Yes, I realize we could call super.test(int), but maybe we want to be able to transparently call the same method without having to worry about exactly what the parameter type is. Furthermore, maybe we don't know exactly how the int version of the method is implemented in the superclass, so we couldn't properly override it even if we wanted to).
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
It is not a matter of whether a method is inherited from a super class or declared explicitly in the sub class. Even if that method was declared explicitly in the sub class, we still could not call test(i) in a static method.
Anshul Manisha
Ranch Hand

Joined: Apr 17, 2001
Posts: 74
Cameron question is not whether we can access method test(i) from static main. Question is why different methods are being invoked. Let me state it again - we have two scenarios
scenario 1 I have not provided method bodies. see above for same

In above scenario method double test(double d) is invoked.
Scenario 2

In above scenario method int test(int i) is invoked. Why there is this discrepancy that is point of discussion. Any insights would be welcome.
regards,

Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
The JLS actually explains it and it has been discussed here before.
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#20448
The way it works is,
1) Q: does the declared type of the object (Super) have a method that supports the request?
A: Yes, because Super has a method called test() that can take an int as a parameter
2) Q: Does the actual type (Sub) override that method?
A: No, Sub has no method that overrides test(double)
Result: run Super.test(double)


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Here's another example:

This will print "super version".
This is why if you are going to override the Object.equals() you must always implement the equals(Object o) and never create an equals() that takes your class as a parameter.
Christophe Lee
Ranch Hand

Joined: Jul 10, 2001
Posts: 142
If the method in the subclass overrid the superclass, as in:
test(Object obj) //this is in the subclass, overridding the super
Then the subclass's method would execute correct?
However, if the method in the SUBclass does not override, and only overload it, then the SUPERclass's method will execute (even though the arg matches the SUBclass's).
Does that summarize it correctly?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
That is correct. The sequence is:
1) Find a method in the specified class that can handle the request
2) determine if that method has been overridden and if it is, use the overriding method
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: help! I can't understand.
 
Similar Threads
probleme in constructor(Help)
Sub class fundu question
Q
Problem with calling the right inherited method
Problem overloading Super class's method in Sub!?