1

I've read many articles on the difference between static binding and dynamic binding in Java, but none of them really talk about why we need dynamic binding. Why is it that some code should only be bonded during runtime? Why can't all code be bonded at compile time?

I understand the concepts of method overridding and polymorphism, but what I don't understand is why the compiler (during compile time) wouldn't be able to discern that an object is from a subclass and should just use that method instead.

Ryn
  • 441
  • 2
  • 10
  • In dynamic systems such as OSGi you don't know which API implementation is available at runtime.Another example is plugin system. – rkosegi Feb 13 '20 at 07:32

1 Answers1

1

Dynamic binding allows the same routine (function, method) to act on different types.it is essential to adhere to Liskov's Substitute Principal

e.g.

Set<Employee> fmList = getEmployees();

You can easily replace the implementation ofgetEmployees() to return a specific type of Set.

https://en.wikipedia.org/wiki/SOLID

How to think about polymorphism with subtyping

Govinda Sakhare
  • 5,009
  • 6
  • 33
  • 74