• 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

help! I can't understand.

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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; }
}
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aahh!
Noooow I get it! Thanks Manfred! You rule!
//Kaspar
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manfred, wouldn't a simple call to "test(i)" invoke the method in the subclass?
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Anshul Manisha
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,

 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic