Table of Contents
Binding in Java
In Java, binding is the connection between a function call to the method body. There are two types of binding in Java – static and dynamic binding. When the binding happens during compile-time, we call it static binding. When the binding happens during run-time, it is dynamic binding. It is important that you should have some basic idea of Inheritance in Java before we proceed.
Different instance types
Before we understand in detail about static and dynamic binding in Java, we need to have knowledge of different instance types.
Here, Employee is a class and e is a type of Employee
class Employee { public static void main(String[] args) { Employee e; } }
Here, e is an instance of Employee class. Since it extends Person class, e is an instance of Person class as well.
class Person { .... } class Employee extends Person { public static void main(String[] args) { Employee e = new Employee(); } }
Here the Person class reference has a child class reference assigned. In other words, p is an Employee object of reference type Person. This is possible since Employee class extends the Person class.
class Person { ... } class Employee extends Person { public static void main(String[] args) { Person p = new Employee(); } }
Static Binding in Java
When the compiler is able to determine the method call binding to the method body during compile-time, we call it static binding or early binding. Generally, the methods which are static, private, and final are static binding. This is because method overriding is not possible for such methods. Hence the compiler knows which class method to call during compile time itself.
Let’s see a simple example of static binding. Here we have only 1 class with a single method and we create a reference object of the Student class. Hence when it calls s.getName, the compiler knows that it belongs to the Student class getName method. There is no confusion in the binding process.
class Student { public void getName() { System.out.println("Student Name"); } } public class StatingBinding { public static void main(String[] args) { Student s = new Student(); s.getName(); } }
Student Name
Now consider the below example. We have a parent class Person with a static method getName and a subclass Employee also with a static method getName. First, we create an object p1 of Person class. When it executes p1.getName, it directly calls the Parent class method. This is because the compiler resolves binding during compile time itself.
Next, we create an Employee object of Person type. Since the parent class method is static, the compiler knows that we cannot override this method. Hence when it executes p2.getName, it calls the Person class method only. This the reason it prints “Person Name” for both the method calls.
class Person { public static void getName() { System.out.println("Person Name"); } } class Employee extends Person { public static void getName() { System.out.println("Employee Name"); } } public class StaticDemo { public static void main(String[] args) { Person p1 = new Person(); Person p2 = new Employee(); p1.getName(); p2.getName(); } }
Person Name Person Name
Dynamic Binding in Java
When the binding between the method call and its associated function happens during runtime, we call it dynamic binding or late binding. This occurs mainly in method overriding. To know more about overriding, please refer to polymorphism in Java.
In method overriding, both superclass and subclasses have the same method name. Based on the object type, which is determined at runtime, it calls the corresponding class method during the binding process.
We can use the same example as above so that we can understand the difference better. The only thing is the methods are not static.
Here, p1 is an Employee object of Person type. Hence p1.getName calls Employee method. This is identified during runtime since methods are not static and the compiler does not know binding process during compile-time. Next, p2 is a Person object and so p2.getName calls Person method. Similarily, e is an Employee object, and e.getName calls Employee method. In these 2 cases, there is no ambiguity in binding process.
class Person { public void getName() { System.out.println("Person Name"); } } class Employee extends Person { public void getName() { System.out.println("Employee Name"); } } public class DynamicDemo { public static void main(String[] args) { Person p1 = new Employee(); Person p2 = new Person(); Employee e = new Employee(); p1.getName(); p2.getName(); e.getName(); } }
Employee Name Person Name Employee Name
Difference between static and dynamic binding in Java
Below are the differences between static and dynamic binding in Java.
Static Binding | Dynamic Binding |
---|---|
Also called early binding or compile-time binding | Also called late binding or runtime binding |
Method overloading is static binding | Method overriding is dynamic binding |
Uses type of class to resolve the binding | Uses type of object to resolve binding |
Faster execution | Slow execution |