• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Overloading vs Overriding

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All
Pg 154 of K&B book for SCJP 5.0 says:

Object type determines which overridden method is used at runtime.Reference type determines which overloaded method is used at compile time.



What does it mean by method invocation happening at compile time & runtime? And why does it happen differently for overloaded & overridden versions ? Can anyone explain this in detail ?

Thank you
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To understand this statement you must first understand what is overloading, overriding and polymorphism.

Evidently, this is not the right place to teach that. But perhaps a couple of examples might help to clarify it.

Let's see an example of overloading a method. The class java.lang.Integer contains these two methods



Therefore, the method parseInt is overloaded. Depending on the arguments that you to this method, the compile can determine which of the methods should be invoked. Evidently, this can be done at compile time, based on the data types of the arguments.

Now, let's see an example of overloading and polymorphism:



As you know String overloads the method toString(). Now, the compiler knows that "s" is an object, but it cannot determine what exact toString() method will be invoked, since "s" could contain any subclass of object. In this case, the toString() method that is invoked is that of the String class, since that is the real object contained in "s". But if you uncomment s = new Integer(), then the method invoked would be that of the Integer class.

The compiler cannot determine for sure, at compile time, the real type of the object, therefore, that is determined at runtime, and the appropriate method is invoked.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition to what Edwin said, I threw together a simple example that illustrates the point further:



While it's easy to see that calling Integer.parseInt("1") can be differentiated from Integer.parseInt("1",16) based on the argument count, and even logically by seeing that an argument list of two ints is different than one of an int and a string, it's important to be aware of the behavior of method binding for overloading when the dynamic types are the same and you might reason that the behavior would be the same. Above, you can see that while the dynamic type of a is the same for both calls to methodA(), method binding occurs at compile time here, so the static type (Animal) wins.

[ May 16, 2008: Message edited by: Marc Bollinger ]
[ May 16, 2008: Message edited by: Marc Bollinger ]
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The complier resolves which overloaded methods to invoke at compile time - early binding.

for overridden methods, the compiler resolves method call at runtime - late binding.
 
Marshal
Posts: 80618
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to JavaRanch, Marc Bollinger.

You're not related to Gregg Bollinger, are you?
 
Swapna Gouri Kalanidhi
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for the detailed response, what you all have said I understood. But I have a doubt which I would like to get clarified...

Which overloaded version is called is determined at the compile time - does this mean that the compiler determines this by looking at the number of arguments & type of arguments being passed to a method?

Also, which overridden version is called is known at runtime to the JVM - does it mean only after interpreting the byte code the JVM comes to know that the method is being overridden ??

But in the case of overloading, unless the bytecode gets interpreted how can the compiler come to know that the method is overloaded but not overridden ?

Can anyone clarify these doubts for me.... thanks all !
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Which overloaded version is called is determined at the compile time - does this mean that the compiler determines this by looking at the number of arguments & type of arguments being passed to a method?



Yes, the compiler determines which signature of the method to call at compile time.


But in the case of overloading, unless the bytecode gets interpreted how can the compiler come to know that the method is overloaded but not overridden ?



I think you are getting confused here. The "compiler" in this case is the java compiler -- not the just in time compiler. By the time you are working with bytecodes, the compiler is already done. But to answer your question....

The overloaded method isn't completely hardwired. Once the method is determined at compile time, the call only goes to an indirect jump. During runtime, the JRE will calculate that jump target, to make sure that overridden methods goes to the right location.

And this doesn't matter whether the bytecodes are interpreted or JIT compiled. The logic is the same.

Henry
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Also, which overridden version is called is known at runtime to the JVM - does it mean only after interpreting the byte code the JVM comes to know that the method is being overridden ??



Oh I see the argument. You are saying that since it is not known until runtime whether a method is overridden, the compiler can't know the exact method that will be called. And you will be right.

The compiler can only determine which overloaded method to call -- which has to be further checked to see if it is overriden at runtime. A subtle argument, but this doesn't mean the compiler can't determine the overloaded method at compile time, it does. It just doesn't know the exact class that the method belongs to.

Henry
[ May 17, 2008: Message edited by: Henry Wong ]
 
Swapna Gouri Kalanidhi
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Henry, I got the point...Thanks a lot!Thanks to all !
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compiler is enough judicious to discriminate between overloaded methods .
We know that in overloaded method there is change in parameter or order of parameter. so compiler can decide which method should be called at compile time.
But so far as overriding is concern ,its return type and parameters remain same ,so compiler can't decide which method should be called..
and its depend on type of object.
(if i am wrong , anyone please correct me).
 
reply
    Bookmark Topic Watch Topic
  • New Topic