Dynamic and static binding
Today, I’ll be sharing one of the important existing facts in Java, which I personally feel that every Java Professional should be aware of as it helps us to understand the Java code to locate and debug / troubleshoot bugs or issues we may come across while running a Java program.
Dynamic Binding
In Java, we can assign derived class object to a base class variable. For example, if you have a class named Vehicle from which you derived the class Car,
Vehicle myVehicle = new Car();
The variable on the left is an object of class Vehicle, but the object on the right is type Car. It will compile and run successfully but reverse is not possible.
If the Car class has a method that is the same as a method in the Vehicle, then the method in the derived Car class will be called.
For instance, if both classes define a method called show(), and you do this:
myVehicle.show();
In this case,the method of show() in the Car class will be called.Even though you are using an Vehicle variable type to call the method show(), the method of show() in the method of Vehicle class won’t be executed. Instead, it is the method of show() in the Car class that will be executed.
The type of the object that is assigned to the Vehicle variable determines the method that is called. So, when the compiler scans the program and sees a statement like this:
myVehicle.show();
It knows that myVehicle is of type Vehicle, but the compiler also knows that myVehicle can be a reference to any class derived from Vehicle. Therefore, the compiler doesn’t know what version of show() that statement is calling. It’s not until the assignment:
Vehicle myVehicle = new Car();
After executing the method of show() is determined. Since the assignment doesn’t occur until runtime, it’s not until runtime that the correct method of show() is known. 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
It occurs when the compiler determines the correct method called from superclass or subclass something during compile time ,i.e. before the program is executed.
If both the Vehicle class and the Car class have a member variable with the same name, it’s the base class version that is used.
Vehicle myVehicle = new Car();
Both the Vehicle and Car classes have a String member variable ‘type’, then if you do this:
String str = myVehicle.type;
The value of ‘type’ can be fully determined by the compiler. Because polymorphism is not allowed for member variables, that statement is referring to the value of ‘type’ in the Vehicle class–not the Car’s value for ‘type’. The result is: with member variables, it’s the type of the variable (e.g. myVehicle) that determines which version is called–not the type of the object the variable refers to (e.g. Car).
That means if both the Vehicle class and the Car class have a member variable with the same name, it’s the base class version that is used.
When the compiler is able to find out the correct version of something during compilation, that is known as “static binding or early binding”.
Below is an example of both dynamic and static binding:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package com.j2eereference.coreJava; class Vehicle { public String type = "Sedan"; public void display() { System.out.println("The vehicle is: " + type); } } class Car extends Vehicle { public String type; //same member variable name as in base class public Car(String type) { this.type = type; } public void display() //same method signature as in base class { System.out.println("The car is: " + type); } } public class DemoBindingExample { public static void main(String[] args) { Vehicle honda = new car("Accord"); honda.display(); // "The car is: Accord" (dynamic binding) System.out.println("The type is: " + honda.type); //"The type is: Sedan" (static binding) } } |
Leave a Reply