DEV Community

Preethi Nandhagopal
Preethi Nandhagopal

Posted on

Dynamic Binding

Dynamic Binding:
Dynamic binding, also known as late binding or runtime polymorphism, is a mechanism where the method call is resolved at runtime rather than compile time. This allows java to support polymorphism behavior, enabling a single interface to represent different underlying forms (data types).

When a method is overridden in a subclass and the subclass object is referred to by a superclass reference, the method call is resolved at runtime. This decision is based on the actual object type, not the reference type. This behavior is facilated by dynamic binding.

Binding refers to linking a method call to its method body. It can be happen at either compile time (static) or runtime (dynamic).

Dynamic binding occurs when a method in subclass overrides a method in its superclass and that method is called through a superclass references pointing to a subclass object.

The JVM uses the object actual runtime type to resolve which overridden method to invoke.

Only instance methods that are overridden and not declared static, final or private are eligible for dynamic binding.

This enables polymorphism, allowing more flexible and maintainable code.

Benefits:
Polymorphism->Allows object to be treated as instances of their superclass, enabling flexible and reusable code.
Extensibility->New classes can be introduced with minimal changes to existing code.
Maintainability->Enhances codes maintainability by adhering to the open/closed principle.

Top comments (0)