BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Static and dynamic binding in java

Last Updated: November 20, 2024 by Chaitanya Singh | Filed Under: java

Static and dynamic binding are basic OOPs concepts. These concepts are associated with the polymorphism. When a method is called in a java program, its body is invoked. This association of method call to the method body is known as binding.

For example: This statement System.out.println("Hello"); is calling the method println(). The body of the method present in the System class.

There are two types of binding: Static Binding that happens at compile time and Dynamic Binding that happens at runtime.

Java static binding and dynamic binding Example

class Animal {
// Static Binding Example
void sound() { // Base class method
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {
// Dynamic Binding Example, here we are overriding the sound()
// method of parent class Animal
@Override
void sound() { // Overridden method (Note method name is same)
System.out.println("Dog barks");
}

// Method specific to child class Dog
void specialBehavior() {
System.out.println("Dog wags tail");
}
}

public class BindingExample {
public static void main(String[] args) {
// Static Binding
Animal animal = new Animal(); // Resolved at compile-time
animal.sound();

// Dynamic Binding
Animal animalDog = new Dog(); // Resolved at runtime
animalDog.sound();

// Note: This will cause a compile-time error since the reference type is Animal
// animalDog.specialBehavior();

// Access Dog-specific behavior
Dog specificDog = new Dog();
specificDog.specialBehavior();
}
}

Output:

Animal makes a sound
Dog barks
Dog wags tail

Explanation

  1. Static Binding:
    • Happens at compile-time.
    • Methods that are not overridden (e.g., animal.sound() when animal is of type Animal) are resolved at compile-time.
  2. Dynamic Binding:
    • Happens at runtime.
    • Overridden methods (e.g., animalDog.sound() when animalDog is of type Animal but refers to a Dog object) are resolved based on the object’s actual type at runtime.

Static binding is used for private, static, and final methods, while dynamic binding is used for overridden methods in polymorphism.

Static and Dynamic Binding in Java

As mentioned above, association of method call to the method definition is known as binding. There are two types of binding: Static binding and dynamic binding. Lets discuss them.

Static Binding or Early Binding

The binding which can be resolved at compile time by compiler is known as static or early binding. The binding of static, private and final methods is compile-time. Why? The reason is that the these method cannot be overridden and the type of the class is determined at the compile time. Lets see an example to understand this:

Static binding example

Here we have two classes Human and Boy, both of these classes have a static method walk(). This is an example of static binding as compiler knows that this method cannot be overriden so it has only one body which is defined in the class Human. The surety of the body makes the compiler works easy as it can make the binding during compilation itself.

class Human{
   public static void walk()
   {
       System.out.println("Human walks");
   }
}
class Boy extends Human{
   public static void walk(){
       System.out.println("Boy walks");
   }
   public static void main( String args[]) {
       /* Reference is of Human type and object is
        * Boy type
        */
       Human obj = new Boy();
       /* Reference is of HUman type and object is
        * of Human type.
        */
       Human obj2 = new Human();
       obj.walk();
       obj2.walk();
   }
}

Output:

Human walks
Human walks

Dynamic Binding or Late Binding

When compiler is not able to resolve the call/binding at compile time, such binding is known as Dynamic or late Binding. Method Overriding is a perfect example of dynamic binding as in overriding both parent and child classes have same method and in this case the type of the object determines which method is to be executed. The type of object is determined at the run time so this is known as dynamic binding.

Dynamic binding example

This is similar to what we have seen above. The only difference here is that in this example, overriding is actually happening since these methods are not static, private and final. In case of overriding the call to the overriden method is determined at runtime by the type of object thus late binding happens.

Here compiler is not sure which walk() method body needs to be invoked during method call as there are two variants of the same method is present, one in parent class and one in child class. This can only be determined during runtime, thus this binding happens at runtime.

Lets see an example to understand this:

class Human{
   //Overridden Method
   public void walk()
   {
       System.out.println("Human walks");
   }
}
class Demo extends Human{
   //Overriding Method
   public void walk(){
       System.out.println("Boy walks");
   }
   public static void main( String args[]) {
       /* Reference is of Human type and object is
        * Boy type
        */
       Human obj = new Demo();
       /* Reference is of HUman type and object is
        * of Human type.
        */
       Human obj2 = new Human();
       obj.walk();
       obj2.walk();
   }
}

Output:

Boy walks
Human walks

As you can see that the output is different than what we saw in the static binding example, because in this case while creation of object obj the type of the object is determined as a Boy type so method of Boy class is called. Remember the type of the object is determined at the runtime.

Static Binding vs Dynamic Binding

Lets discuss the difference between static and dynamic binding in Java.

  1. Static binding happens at compile-time while dynamic binding happens at runtime.
  2. Binding of private, static and final methods always happen at compile time since these methods cannot be overridden. When the method overriding is actually happening and the reference of parent type is assigned to the object of child class type then such binding is resolved during runtime.
  3. The binding of overloaded methods is static and the binding of overridden methods is dynamic.
❮ PreviousNext ❯

Top Related Articles:

  1. StringJoiner toString() Method in Java
  2. Java – Static Class, Block, Methods and Variables
  3. Inheritance in Java With Examples
  4. How to write to file in Java using BufferedWriter
  5. Inner classes in java: Anonymous inner and static nested class

Tags: Java-OOPs

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Ashish says

    May 30, 2013 at 7:24 PM

    I was searching for static vs dynamic binding and found this, very helpful for me. Thank you so much for explaining these concepts with examples

    Reply
    • Chaitanya Singh says

      May 30, 2013 at 7:38 PM

      I am glad that you liked it. let me know if you have any question regarding binding in java, I would be happy to help you out.

      Reply
  2. Harry says

    May 30, 2013 at 7:33 PM

    I understood the above points about early and late binding, however can you please explain binding in terms of references? It would be really helpful for me.

    Reply
    • Chaitanya Singh says

      May 30, 2013 at 7:45 PM

      Harry – let me explain your doubt with the help of few examples –
      Static binding:
      it means that the references are resolved during compile time by compiler
      lets say –

      Car obj = new Car();
      obj.drive();

      Compiler won’t face any difficulty while resolving the conflict during compilation.

      Dynamic Binding:
      Another example –

      public void myMethod(Object obj){
      ((Car)obj).drive();
      }

      In this case compiler can’t resolve the object reference during compilation. Hence this can be resolved during run time only. This is called dynamic binding.

      Reply
  3. praveen says

    December 23, 2013 at 4:10 AM

    Iamnot clear with the explaination actually do you mean dynamic polymorphism and dynamic binding are same

    Reply
  4. Akshay Hendre says

    April 1, 2014 at 6:18 AM

    Hi Chaitanya, as you say in dynamic binding that, compiler gets confused while choosing method either from parent class or from child class. But how does it make right choice?

    Reply
    • Chaitanya Singh says

      April 6, 2014 at 3:31 PM

      Hi Akshay, References can only be resolved at runtime. Lets take the above example to understand this:

      Human myobj = new Boy();

      The fact that myobj is refers to the object of its child class Boy is resolved at runtime only. Hence, at compile time the compiler can’t be sure if the call to the method ‘walk()’ on these references actually refer to which version of the method – the super class version or the sub class version. That’s the reason such type of binding happens at runtime and known as dynamic binding.

      Reply
      • Saddam Khan says

        January 27, 2015 at 1:00 PM

        If the references variables are resolved at runtime then what will you call this below:

        A ob = new ob():
        ob.Show();

        so here there is nothing ambiguous, so what should we call it early or late binding?

        Reply
  5. Rajani says

    April 24, 2014 at 9:58 AM

    hi Chaitanya , your tutorial was very nice. Thank you so much for woderful explanation with examples.

    Reply
  6. Deeraj says

    May 16, 2014 at 9:35 AM

    Very neatly explained, Thanks :)

    Reply
  7. Ahmad soory says

    June 12, 2014 at 3:34 PM

    Hi
    Thanks for this article
    I have a question. What’s mean method binding??

    Reply
  8. Shahrukh Dilshad says

    June 14, 2014 at 6:30 PM

    It is really helpful Thanks..

    Reply
  9. anshu says

    June 17, 2014 at 5:35 PM

    this is helpful

    Reply
  10. srinivas says

    June 24, 2014 at 2:01 AM

    very good example and content

    Reply
  11. Poonam says

    August 29, 2014 at 5:42 AM

    Very well explained with examples.
    Very helpful , informative and to the point answer.

    Reply
  12. Ameya says

    October 21, 2014 at 2:15 PM

    Just the thing I was looking for… Thanks a lot!

    Reply
  13. Faezeh says

    November 3, 2014 at 9:39 AM

    Hi,
    Thank you for the explanation. I wrote a private method in my super class and I was able to override it in my subclass. I also, wrote a static method in my super class and I was able to override it in my subclass. Could you please tell me what do you mean by saying that static and private methods cannot be overridden?

    Thanks

    Reply
  14. vikas says

    November 11, 2014 at 5:32 AM

    i can say only one thing ………….. Entire Site is “AWESOME”.

    Reply
  15. Cam says

    November 15, 2014 at 8:23 PM

    It is a wonderfully clear explanation!

    Reply
  16. Nandhakumar says

    December 3, 2014 at 12:11 AM

    Hi

    I have been confused for long time,after found this article i came to conclusion about binding..Super job ..tx a lot.

    Reply
  17. vikas says

    December 8, 2014 at 10:05 PM

    Hi,
    So am I right if I say – binding of non-private instance method is done at runtime. Is there any Java spec which confirms the same.

    Reply
  18. Subhadeep Das says

    December 26, 2014 at 7:26 AM

    It’s said that Java uses static binding for overloaded methods. Please give an example to make it a bit clearer to me..

    Reply
  19. Saddam Khan says

    January 27, 2015 at 12:57 PM

    I read your short article it is written quite nicely,
    I want to ask what will you call the overloaded methods?
    static binding or late binding?
    according to you static binding comes where there is no ambiguity that which method to call, but in overriding a parent class variable is ambiguous to receive an object from which child class.
    Don’t you think similarly in overloaded method its also ambiguous untill unless an argument is passed in runtime and certain method gets called,

    Then why overloading is attributed as static binding? early binding?

    Reply
  20. veron says

    February 9, 2015 at 6:55 AM

    I wasn’t getting a clear understanding of static and dynamic binding but after this article I’ve gotten a clear understanding of binding. tanx a lot

    Reply
  21. tanuja jain says

    March 13, 2015 at 1:31 PM

    Is there any way in java through which we can find the debugging process of a program. I really need to know the compilation process about what get’s compiled first and what after that. Someboby please help me!

    Reply
  22. chandu says

    March 23, 2015 at 12:44 PM

    Hello,
    Its a nice artice.Have a question regarding resolving references.
    I would like know what made the run time resolve the reference which compiler couldn’t do it.
    I mean why does compiler is able to resolve the reference for
    Dog d = new Dog()

    and compiler cant resolve the reference for
    Animal d = new Dog().
    And why at run time references are resolved>Is it because of object creation?

    Reply
  23. luis says

    March 30, 2015 at 5:34 AM

    Thanks for the explanation.

    I have one question. If at the compile time, the compiler doesn’t know if the walk method is of Human or Boy, how does it know at runtime? Or if the compiler doesn’t know then who knows?

    Thanks a lot!

    Reply
  24. luis says

    March 30, 2015 at 5:44 AM

    This article on cpp binding will provide extra insights into binding. It helped me understand better.

    Reply
  25. Ravindra says

    April 2, 2015 at 4:30 AM

    what is the benefit of dynamic binding over static one.. if we want to access the methods of sub class then we can make an object of the same with reference variable of sub class type…. what is the benefit if we want to call a method of sub class and using reference variable of super class type?

    Reply
  26. Aks says

    April 24, 2015 at 6:53 AM

    Hi Chaitanya,
    Thanks for your tutorial. In the example you have given
    Human myobj = new Boy();
    myobj.walk();
    and the output will be Boy walks i.e walk method of Boy class is called. What if we want to call walk method of Human class ? How will we call a parent class method in a child class?

    Reply
    • java says

      May 25, 2015 at 7:50 AM

      u can call it with the help of super keyword

      Reply
  27. Praveen Chukkapalli says

    June 10, 2015 at 5:10 PM

    I am very new to programming languages and I barely have knowledge about these. I’m just following your tutorial and able to understand very clearly. Thank you so much for this explanations….,

    Reply
  28. budy says

    August 31, 2015 at 5:39 PM

    one of the best tutor of Java…

    Reply
  29. Neeraj says

    November 11, 2015 at 7:11 PM

    Thanks Chaitanya, i have just started learning java, I got lot of my queries resolved by reading here.
    Keep it up…
    Thanks.

    Reply
  30. Dhivya says

    December 11, 2015 at 2:19 AM

    public void myMethod(Object obj){
    ((Car)obj).drive();
    }

    What is meant by passing the object at runtime, in the above example, the object is passed at runtime , but it is possible only in method-overriding and not in method-overloading? pl do give a reply

    Reply
  31. Nithesh Reddy says

    May 11, 2016 at 10:56 AM

    I am getting confused at overriding of static method. will they come under static polymorphism?

    Reply
  32. shefali says

    June 29, 2016 at 6:36 AM

    static and dynamic binding is explained v nicely!!!
    Thanks!!!!!

    Reply
  33. Rishika says

    August 20, 2016 at 11:56 AM

    you have called the method as
    Human myobj = new Boy();

    why can’t i have
    Boy myobj = new Boy();
    why wouldn’t the compiler know that i am trying to access the child class. what is the exact need for dynamic binding?

    Reply
  34. Sebastian says

    January 3, 2017 at 8:48 AM

    Thanks!
    Perfectly explained! :)

    Reply
  35. Mano says

    February 22, 2017 at 8:34 PM

    Never mind . My bad. Got it.

    Reply
  36. Sarvesh Gupta says

    August 18, 2017 at 6:58 AM

    class A{

    }
    class B extends A{

    public void func(){
    System.out.println(“Funtion in B”);
    }

    public static void main(String[] g){
    B b=new B();
    b.func();
    A a=new B();
    a.func();

    }
    }

    Author, please explain why compiler gives error here over Var type A.
    I mean if the virtual methods(here func()) are bonded at runtime why is there an error here.
    Its completely okay it func() was static or final or private but why with virtual func().
    That might sound a silly question but I am confused.
    Please help!

    Reply
    • Chaitanya Singh says

      August 18, 2017 at 8:43 AM

      Hello Sarvesh,
      For this to work you must have func() method in parent class(class A) as well, otherwise it will throw compilation error as method func() is undefined for the class A.

      Reply

Leave a Reply Cancel reply

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

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap