Difference between early binding and late binding in java

Binding in Java refers to the process of associating a method or function body with a method or function call by the Java Compiler. In simpler words, when a function is called by the Java compiler the task of correctly locating the method for the respective function is binding. Depending on when the compiler is able to associate a method body with its method call, binding in java is classified into two main headers:

  • Early Binding, also known as Static Binding
  • Late Binding, also known as Dynamic Binding

Let us go through these two types in detail.

Early Binding or Static Binding

As mentioned earlier, binding refers to the process of associating or “binding” a method body with its call. Early or Static Binding refers to the association of these two entities at compile time by the Java Compiler. Static binding is used to associate any private, final or static method bodies with their method call statements prior any execution taking place.

Why are static final and private methods associated by Early Binding?

This is due to the fact that static, final or private methods cannot be overridden by any subclass and hence they are always referred to by a constant reference, i.e. the class in which they are defined. Due to this property of static, final or private methods, the java compiler can easily determine pre-execution that those methods can only be referenced by an object of the class they are defined in.

The following example shall demonstrate Static Binding.

Code Example:

Output:

Car Engine Started
Car Engine Started

Analysis:

If we analyze the output of the above code, we notice that despite overriding the “startEngine” method in derived class Porsche and initializing the object “car1” with the reference of the class Porsche, the method “startEngine()” method was not overridden and printed the text of the superclass Car.

This was due to the fact that the static method “startEngine()” of the class Car cannot be overridden and was bound by the java compiler during compilation.

Now let us have a look at the other form of Binding:

Late Binding or Dynamic Binding

If the compiler is unable to determine which method call a particular method is bound during compilation, then it resorts to late binding or dynamic binding. The best instance of dynamic binding is Method Overriding. A subclass overrides the method of the super class and during execution, methods get associated with their respective references.

It may be noted here that that for Dynamic Binding to occur, it is imperative that the method to be overridden is not declared as static, final or private. Declaring the method with any of the above modifiers will prevent it from being overridden and java will resort to static binding as it can easily determine the parent reference.

Let us observe dynamic binding by using the previous example. Only this time, the methods will no longer be declared as “static”

Code Example:

Output:

Porche’s Engine Started
Car Engine Started

Analysis:

The difference in output is noticeable, as the first method call has successfully bound the reference of the class Porsche and called the overridden method “startEngine()”. As we have no longer used the static keyword as a modifier to the method, the java compiler could not find a reference to bind the method to during compilation and hence resorted to Dynamic binding and the type or reference of the method was determined at runtime.

Differences between Static and Dynamic Binding

Static BindingDynamic Binding
Static or Early Binding takes place at compile time.Dynamic or Late Binding takes place during runtime
Methods declared as private, static or final show static binding as they cannot be overridden and can be associated, during compilationMethods which are public, protected or default show dynamic binding as they can be overridden and their reference is associated only at runtime
Actual object reference is not used in early bindingActual object reference is used
Method overloading is the best example of Static or Early BindingMethod Overriding is the prime example of Dynamic or Late Binding

Related Posts

  • 04 November

    Can we overload main method in java

    In this post, we will see about "Can we overload main method in java".This is one of the most asked Core java interview questions. Yes, we can overload main method in java but when you run your program, JVM will search for public static void main(String[] args) and execute that method. Overload main method in […]

  • 02 November

    Dynamic method dispatch in java

    In this post, we will about Dynamic method dispatch which is also referred as run time polymorphism. Dynamic Method Dispatch Dynamic method dispatch is a technique by which call to a overridden method is resolved at runtime, rather than compile time.When an overridden method is called by a reference, then which version of overridden method […]

  • 29 October

    Can we override static method in java

    No, we can not override static method in java. Static methods are those which can be called without creating object of class,they are class level methods. On other hand,If subclass is having same method signature as base class then it is known as method overriding. Its execution decided at run time. Below are the reasons why […]

  • 26 August

    Copy Constructor in java

    In this post, we will see about copy constructor in java. Copy constructor is the constructor which takes parameter as object of same class and copy each field of the class to the new object. Unlike C++, java does not provide default copy contructor. You need to create one, if you want to have copy […]

  • 26 August

    Java default constructor

    In this post, we will see about Java default constructor. Default constructor is the no arg constructor which is inserted by compiler unless you provide any other constructor explicitly. You won’t able to see it as it is present in class file rather than source file. Is there any difference between no argument constructor and […]

  • 28 July

    Inheritance in Java

    In this post, we will see about Inheritance in java. It is one of the OOPs principles apart from Abstraction, encapsulation and polymorphism. Introduction The word Inheritance is quite familiar with everyone. In common terms, the word means the bequeathing of property and characteristics from generation to generation. For example, the property or characteristics of […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.