• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

overloading ...

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would have thought, that the given program will compile fine, but generate a runtime error. As usual I am wrong
The output of the program is
A.m1:999999
Can anyone explain why?

class A {
public void m1(int i) {
System.out.println("A.m1:"+i);
}
}
class B extends A {
public void m1(long i) { // This method is overloaded, right?
System.out.println("B.m1:"+i);
}
}
class test5 {
public static void main(String[] s) {
A a = new B();
a.m1(999999);
}
}
Thanks,
Cathy.
 
Ranch Hand
Posts: 218
VI Editor Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cathy,
It's because the value you passed in is 999999, which is still under
the range of int data type. So by default it call the method with
int parameter. If you force your parameter as a long with 999999L, it
will call the class B version of the method.
Hope that helps..
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I tried the above program with 999999L and i tried compiling the program.
I got an error:
m1(int) in A cannot be applied to (long)
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason is

Overloaded

method to call is based on Reference type passed at compile time.
Here, compiler look at object reference "a" as type of class A, it will try to find an overloaded method m1(long i) in class A. Unfortunately the compiler can not find any method named "m1" accepts a parameter with type of "long" in class A.
We know, at compile time, compiler does not know to which kind of object "a" really points. Only at runtime, JVM will find out "a" really referenced to object of type B instead of its parent A.
Hope this will help.
Correct me if anything wrong.
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

We know, at compile time, compiler does not know to which kind of object "a" really points. Only at runtime, JVM will find out "a" really referenced to object of type B instead of its parent A.


To continue with the above reasoning, at runtime, the JVM sees 2 methods of object type B, one with int and the other with long. Then how come at runtime, we dont get an error?
Thanks,
Cathy.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cathy,
Yes, JVM sees two methods at runtime, but it is too late since the compiler has already binded the method of reference type A for "a".
To summarise:
- Overloaded methods are resolved/binded at compile time with the reference type.
- Overriden methods are resolved at runtime with the actual object the reference is 'pointing' to.
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Unni.
 
I like tacos! And this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic