File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes OverLoading and Overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "OverLoading and Overriding" Watch "OverLoading and Overriding" New topic
Author

OverLoading and Overriding

Vineela Devi
Ranch Hand

Joined: Dec 20, 2003
Posts: 191
hai,
anyone pls explain why this happens?
class superclass{
void method(int i){
System.out.println("Method 1");
}
void method(float a){
System.out.println("Method 2");
}
public static void main(String args[]){
superclass sc = new superclass();
sc.method(1); // ok compiles and prints "Method 1"
subclass sub = new subclass();
sub.method(2); //line 2
}
}
class subclass extends superclass{
void method(long a){
System.out.println("child Method");
}
}


At line 2 compiler reports ambiguity of the methods. can u pls explain why it happens?
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
I don't see any reason at all why the following code won't compile since the method "method()" is successfully overloaded in both classes.
As a matter of fact i tried the code you posted and it run without any error printing "Method1 Method1" respectively.
Are you sure you posted the write code?!!!


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
Vineela Devi
Ranch Hand

Joined: Dec 20, 2003
Posts: 191
Hi vicken,
Compiler reports the follwing error when we try to compile the code given
>javac superclass.java
superclass.java:12: reference to method is ambiguous, both method method(int) in
superclass and method method(long) in subclass match
sub.method(2); //line 2
^
1 error
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
That is strange!!! I compiled and run it with JDK_1.4.2 and it didn't issue any error, what the JDK you are using?
Harsha Vardhan Madiraju
Greenhorn

Joined: Dec 19, 2003
Posts: 24
Hi Vicken,
The code posted by Vineela is giving compile time error!
(Running on JDK 1.4.2)
U try to copy paste this below code and compile it. This code also generates the same error

class Test
{
void get(int i)
{
System.out.println("Hsssi");
}
void get(float i)
{
System.out.println("saa");
}
}
class Test1 extends Test
{
void get(float x)
{
System.out.println("AAAA");
}
public static void main(String args[])
{
Test1 t = new Test1();
t.get(33);
}
}

Anyone pls explain this...
Thanx in advance
Harsha Vardhan Madiraju
Greenhorn

Joined: Dec 19, 2003
Posts: 24
hi vicken,
Oops!
Im running on JDK 1.4.1_02!
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Neither of the codes posted by Harsha and Vineela gives a compile time error, maybe it was a bug in previous releases and they'd fixed it.
Beside why do you think that it will give an error, all I see is a successful overload for both the methods ("get" and "method").
Or perhaps my compiler is acting weird. :roll:
Harsha, try using the latest release and let me know what happens, cause frankly I am confused.
Vineela Devi
Ranch Hand

Joined: Dec 20, 2003
Posts: 191
hi vicken,
ths the problem we had.coz we also thought there is succeccful
overloading and didnt understand why the compiler is reporting error.
vineela
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
Yes this seems like a bug.
See similar posts.


SCJP 1.4<br />SCWCD 1.4
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Originally posted by dennis zined:
Yes this seems like a bug.
See similar posts.

Thanks for the info Dennis, I thought that too. I searched SUN bug database and i couldn't find anything. Perhaps i didn't look hard enough... who cares as long as you tested it, right?
Good job.
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
Hi Vicken. I came across this type of code when I couldnt believe it was a compile-time error in one of Whizlab's mock exam. After testing the code on two different jdks and working it out with Whizlabs tech support, it was determined it was not a compile-time error in Sun's latest jdk. I tried searching for it too in Sun's database but couldnt find anything. Whizlab's said they will try to get answers from Sun and would inform me once they get answers but havent heard from them yet. So I used the statement "seems like a bug" since there are no confirmations yet (even though its pretty obvious) .
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: OverLoading and Overriding