Dynamic v/s Static Binding

Dynamic v/s Static Binding


Dynamic V/s Static Binding Notes

Dynamic binding and static binding have to do with inheritance. In Java, any derived class object can be assigned to a base class variable. For instance, if you have a class named Animal from which you derived the class Dog, you can do this:

Animal myAnimal = new Dog();

If the Dog class has a method that is the same as a method in the Animal class, then the version of the method in the Dog class will be called. For instance, if both classes define a method called show(), and you do this:

  myAnimal.show();

the version of show() in the Dog class will be called. The type of the object that is assigned to the Animal variable determines the method that is called.. That is known as "dynamic binding" or "late binding": it's not until your program performs some operation at runtime that the correct version of a method can be determined. In Java, most uses of inheritance involve dynamic binding.

"Static binding" or "early binding" occurs when the compiler can readily determine the correct version of something during compile time, i.e. before the program is executed. In Java, member variables have static binding because Java does not allow for polymorphic behavior with member variables. That means if both the Animal class and the Dog class have a member variable with the same name, it's the base class version that is used. For instance, if you set up the necessary ingredients for polymorphic behavior:

Animal myAnimal = new Dog();

and both the Animal and Dog classes have a String member variable 'type', then if you do this:

String str = myAnimal.type;

the value of 'type' can be fully determined by the compiler. Because polymorphic behavior is not allowed for member variables, that statement is referring to the value of 'type' in the Animal class--not the Dog's value for 'type'. When the compiler is able to figure out the correct version of something during compilation, that's known as static binding.

Here is an example of both dynamic and static binding:

class Animal
{    public String type = "mammal";
    public void show()
    {
        System.out.println("The animal is a: " + type);
    }
}

class Dog extends Animal
{    public String type;  //same member variable name as in base class
    public Dog(String type)
    {
        this.type = type;
    }
    public void show()  //same method signature as in base class
    {
        System.out.println("The dog is a: " + type);
    }
}

public class DemoStaticBinding 
{    public static void main(String[] args) 
    {
        Animal doggie = new Dog("daschund");
        doggie.show(); // "The dog is a: daschund"  (dynamic binding)
        System.out.println("The type is: " + doggie.type); //"The type is: mammal" (static binding)
    }
}