In this post, we will learn the difference between Early (Static) Binding and Late (Dynamic) Binding in Java. This is a frequently asked question in Java interviews for beginners. Let's dive into it.
Early (Static) Binding | Late (Dynamic) Binding |
---|---|
In early binding, the binding of method call to its definition happens at compile time. | In late binding, the binding of method call to its definition happens at runtime. |
The compiler knows which method will be called. | The compiler does not know which method will be called. This decision is made at runtime, based on the actual object that is being referenced. |
Because the compiler has all information about the object, it can optimize the code better. | Because the method to be invoked is determined at runtime, the compiler cannot optimize the code as well as in static binding. |
Static methods, final methods, private methods, and methods in final classes all use early binding. | Overridden methods in Java use dynamic binding. |
Polymorphism does not play any role in early binding. | Polymorphism is fully utilized in late binding. |
Early binding is also known as method overloading or static polymorphism. | Late binding is also known as method overriding or dynamic polymorphism. |
Examples include method overloading, operator overloading. | Examples include virtual functions, function overriding. |
It is less flexible as all types need to be known at compile-time. | It is more flexible as types can be determined during runtime. |
Interview Questions
Java
X vs Y
Comments
Post a Comment