• Post Reply Bookmark Topic Watch Topic
  • New Topic

Overloading with primitive types  RSS feed

 
Muthu Kumar Subbiah
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
First post here. This question could have been previously asked. I came across the following scenario with overloading recently and I am unable to figure out why the following is happening.
For the below code, when I execute it, it prints
Short method 10 //result 1
Sub class short method 10 //result 2

Which is as expected but if I comment out line 3, then it prints
Integer method 10 //result 3
Integer method 10 //result 4

I can understand result 3 is because of an upcast from short to int, since FunWithOverloading will not have a overloaded method with short now. However, what is happening with result 4? Shouldn't it call methodA of the subclass with the argument type short? If its because I have declared the reference variable, derived, of the type FunWithOverloading, then how come the first result correctly picks the overloaded method of the sub class?
Can someone help me with this?



 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to be clear, you are doing both overloading and overriding. Overloading is done at compile time -- ie. the compiler will resolve to which method to call. Overriding is done at both compile time and runtime -- ie. the compiler will resolve to which method to call, which is further resolved at runtime to the correct version of that method to call.

Muthu Kumar Subbiah wrote:I can understand result 3 is because of an upcast from short to int, since FunWithOverloading will not have a overloaded method with short now. However, what is happening with result 4? Shouldn't it call methodA of the subclass with the argument type short? If its because I have declared the reference variable, derived, of the type FunWithOverloading


No. From you understanding of three, then you know that at compile time, it can't resolve to the short method. It is no different with four, it can't resolve to that method at compile time.

Muthu Kumar Subbiah wrote:
then how come the first result correctly picks the overloaded method of the sub class?


Well, in the first set of examples, the compile correctly resolves the method at compile time, which is then further resolved at runtime. Basically, if the method isn't resolved at compile time, then the runtime mechanism won't help here.

Henry
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then you know that at compile time, it can't resolve to the short method


Why not? If I have a superclass and a subclass:


I think what's happening is that the short is getting extended to an int and running as the int method.
At compile time, it should find the short method -- why wouldn't it? It's visible, it has the correct signature.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janeice DelVecchio wrote:
At compile time, it should find the short method -- why wouldn't it? It's visible, it has the correct signature.


I was referring to the second case, where the OP "comment out line 3", thus removing the overloaded method (with the short argument) from the base class. In this case, the method does exist in the base class, and hence, is not visible if called used the base class reference.

Henry
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you remove the "line 3" method, I agree that you don't have a method that takes a short in the reference. But you have one in the object itself, right? Won't the correct polymorphic method be chosen at runtime?

I consider myself "Good at Java" (patent pending) -- but this isn't clear to me.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janeice DelVecchio wrote:If you remove the "line 3" method, I agree that you don't have a method that takes a short in the reference. But you have one in the object itself, right? Won't the correct polymorphic method be chosen at runtime?


Unfortunately, no. The method that takes a short does not override the method that takes an int. During compile time, the only method that is visible is the method that takes an int. And during runtime, the only thing that is happening is to determine the correct overridden method -- and no overriding is happening here.

Henry
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Totally not confusing. ;)
 
Muthu Kumar Subbiah
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry. Thanks Janeice. This was really tricky one but I think Henry's reply where he mentioned,
"Overloading is done at compile time -- i.e. the compiler will resolve to which method to call. Overriding is one at both compile time and runtime -- i.e. the compiler will resolve to which method to call, which is further resolved at runtime to the correct version of that method to call."

clears it up. Changing the status of this post to resolved.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!