Understanding Java Dynamic Binding: A Key to Polymorphism
Explore Java dynamic binding, a crucial concept in polymorphism that links method calls to their actual implementations at runtime. This guide delves into how dynamic binding enhances flexibility in object-oriented programming, allowing objects to exhibit various behaviors while maintaining a consistent interface.
Java - Dynamic Binding
Binding is a mechanism that creates a link between a method call and its actual implementation. According to the polymorphism concept in Java, an object can have many different forms, and these forms can be resolved at compile time or run time.
Java Dynamic Binding
Dynamic binding refers to the process in which the link between a method call and its implementation is resolved at run time. This process is also known as run-time polymorphism or late binding. Dynamic binding uses objects to resolve binding.
Characteristics of Java Dynamic Binding
- Linking: The linking between a method call and its implementation is resolved at run time.
- Resolve Mechanism: Dynamic binding uses the object type to resolve binding.
- Example: Method overriding is a classic example of dynamic binding.
- Type of Methods: Virtual methods utilize dynamic binding.
Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.
Example of Java Dynamic Binding
In this example, we create two classes, Animal
and Dog
, where the Dog
class extends the Animal
class. In the main()
method, we use an Animal
class reference to assign it an object of the Dog
class to demonstrate the dynamic binding effect.
Syntax
package com.tutorialsarena;
class Animal {
public void move() {
System.out.println("Animals can move");
}
}
class Dog extends Animal {
public void move() {
System.out.println("Dogs can walk and run");
}
}
public class Tester {
public static void main(String args[]) {
Animal a = new Animal(); // Animal reference and object
// Dynamic Binding
Animal b = new Dog(); // Animal reference but Dog object
a.move(); // runs the method in Animal class
b.move(); // runs the method in Dog class
}
}
Output
Animals can move
Dogs can walk and run
In the above example, even though b
is a type of Animal
, it runs the move
method in the Dog
class. This is because, at compile time, the check is made on the reference type. However, at runtime, the JVM figures out the object type and executes the method that belongs to that specific object.
Java Dynamic Binding: Using the super Keyword
When invoking a superclass version of an overridden method, the super
keyword is used to utilize the parent class method while using dynamic binding.
Example: Using the super Keyword
In this example, we create two classes, Animal
and Dog
, where the Dog
class extends the Animal
class. The Dog
class overrides the move
method of its superclass, Animal
, and calls the parent move()
method using the super
keyword. This allows both move
methods to be called when the child method is invoked due to dynamic binding.
Syntax
class Animal {
public void move() {
System.out.println("Animals can move");
}
}
class Dog extends Animal {
public void move() {
super.move(); // invokes the super class method
System.out.println("Dogs can walk and run");
}
}
public class TestDog {
public static void main(String args[]) {
Animal b = new Dog(); // Animal reference but Dog object
b.move(); // runs the method in Dog class
}
}
Output
Animals can move
Dogs can walk and run