How Java Compiler Generate Code for Overloaded and Overridden Methods?

Here is a simple Java example showing Polymorphism: overloading and overriding.

Polymorphism means that functions assume different forms at different times. In case of compile time it is called function overloading. Overloading allows related methods to be accessed by use of a common name. It is sometimes called ad hoc polymorphism, as opposed to the parametric polymorphism.

class A {
	public void M(int i){
	public void M(String s){
		//this is an overloading method
class B extends A{
	public void M(int i){
		//this is overriding method
		System.out.println("overriden int");
public static void main(String[] args) {
	A a = new A();
	A b = new B();

From the compiler perspective, how is code generated for the correct function calls?

Static overloading is not hard to implement. When processing the declaration of an overloaded function, a new binding maps it to a different implementation. During the type checking process, compiler analyzes the parameter's real type to determine which function to use.

Dynamic overloading allows different implementations of a function to be chosen on the run-time type of an actual parameter. It is a form of dynamic dispatch.

Dynamic dispatch is also used to implement method overriding. The overridden method are determined by real object type during run-time.

To understand dynamic dispatch, there is a post about object layout in memory.

Category >> Common Methods  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";
  • CarlosXu

    “Overloading allows related methods … ” I think what’s your actually meaning is Overriding .