How AlmaBetter created an
IMPACT!Tapash Kumar Mahato
Web Development Consultant at almaBetter
Learn about dynamic binding in Java with practical examples. Understand how runtime polymorphism works and explore real-world applications for efficient coding.
Before diving into static and dynamic binding, it's essential to understand the concept of binding in Java. Binding refers to the process of linking a method call to its corresponding method implementation. It determines which method implementation is invoked when a method is called. Binding can occur either at runtime or at compile time.
Binding is closely related to polymorphism in Java, which is the ability of objects of different classes to be treated as objects of a common superclass. Polymorphism is achieved through inheritance and method overriding, allowing a subclass to provide a specific implementation of a method already defined in its superclass. Whether binding is static or dynamic depends on the type of reference variable used to invoke the method.
Types of Binding in Java : In Java, there are two main types of binding:
In this article, we will be talking about Dynamic Binding in Java.
Static binding, also referred to as early binding, resolves method calls at compile time. This means that the method to be executed is determined during compilation based on the type of the reference variable, rather than the object it points to. It is typically used when the method is defined in the class and there is no method overriding in the subclass. Static binding ensures efficiency in method invocation by resolving calls upfront, aiding in compiler optimizations.
On the contrary, dynamic binding, known as late binding, is a core concept in object-oriented programming, especially in languages like Java. It involves resolving method calls and variable references during runtime rather than compile time. Dynamic binding is pivotal for enabling polymorphism, allowing objects of different classes to be treated uniformly as objects of a common superclass. This flexibility facilitates extensible and maintainable code, enabling seamless addition of new subclasses without modifying existing code that employs superclass references.
Binding in Java can be categorised into two types:
class StaticBinding {
private void display() {
System.out.println("Static Binding Example");
}
public static void main(String[] args) {
StaticBinding obj = new StaticBinding();
obj.display(); // Method call resolved at compile time
}
}
Example:
class StaticBindingExample {
private void display() {
System.out.println("Static Binding Example: Display method");
}
public static void main(String[] args) {
StaticBindingExample obj = new StaticBindingExample();
obj.display(); // Resolved at compile time
}
}
Output:
Example:
Static Binding Example: Display method
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
public class TestDynamicBinding {
public static void main(String[] args) {
Animal obj = new Dog();
obj.sound(); // Method call resolved at runtime
}
}
This dynamic binding in Java example illustrates how the `sound` method in the `Dog` class overrides the `sound` method in the `Animal` class, and the actual method called is determined at runtime based on the object type.
Example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
public class DynamicBindingExample {
public static void main(String[] args) {
Animal obj = new Dog();
obj.sound(); // Resolved at runtime
}
}
Output:
Example:
Dog barks
Try out our latest free online tool Java Compiler!
Aspect | Static Binding | Dynamic Binding |
---|---|---|
Time of Binding | Compile-time | Runtime |
Method Type | Static, private, or final | Non-static and overridden methods |
Efficiency | More efficient due to early resolution | Slightly slower due to runtime lookup |
Example | Static methods or compile-time constants | Overridden methods in polymorphism |
The super keyword in Java is used to refer to the immediate parent class's method or constructor. In the context of dynamic binding, the super keyword ensures that the method call is resolved to the parent class's implementation, bypassing the overridden version in the subclass.
Example:
class Parent {
void display() {
System.out.println("Parent display method");
}
}
class Child extends Parent {
@Override
void display() {
System.out.println("Child display method");
}
void invokeParentDisplay() {
super.display(); // Calls Parent's display method
}
}
public class SuperKeywordExample {
public static void main(String[] args) {
Child obj = new Child();
obj.display(); // Resolved at runtime, calls Child's method
obj.invokeParentDisplay(); // Calls Parent's method using super
}
}
Output:
Example:
Child display method
Parent display method
Dynamic binding, a key feature of object-oriented languages like Java, operates by determining the actual method to execute during runtime, contingent upon the object referenced by the variable. This mechanism relies on virtual method tables (vtables) for instance methods. When a method call is made on an object, the Java Virtual Machine (JVM) consults the object's class's vtable to identify and execute the corresponding method implementation. This dynamic lookup enables polymorphism, allowing for the uniform treatment of objects of different classes through superclass references, enhancing code flexibility and maintainability.
Read our latest blogs “Advantages and Disadvantages of Java” and “Features of Java”
To define dynamic binding in Java, it is the process where method calls are resolved at runtime rather than at compile time, enabling polymorphism and dynamic method invocation. Through the Java dynamic binding mechanism, developers can write more flexible and maintainable code, leveraging the principles of object-oriented programming. The dynamic binding in Java with examples provided above demonstrates how overridden methods are determined at runtime, showcasing the power and necessity of this concept in Java applications.
Enroll in our Full Stack Web Developer course to master skills like these and excel in your tech career!
Related Articles
Top Tutorials