• 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

OverLoading and Overriding

 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?!!!
 
Vineela Devi
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi vicken,
Oops!
Im running on JDK 1.4.1_02!
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes this seems like a bug.
See similar posts.
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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) .
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic