Polymorphism allows for the writing of future-proof, succinct code by dynamically deciding method implementation to execute during run-time.
Consider the equals method. Since every object has the equals method, as every class is a subclass of the Object class, we can implement method overriding to alter the behaviour of the equals method in the subclasses.
Dynamic Binding
Example
Consider the following case:
The foo method is overridden and overloaded as such.
A::foo(A)A::foo(B)B::foo(C)C::foo(B)
Consider the dynamic binding for the following:
A c = new C();B b = new B();c.foo(b);
Find the compile-time type of the target.
The compile-time type of target is A.
List all the methods available to invoke.
A::foo(A), A::foo(B)
Choose the most specific method.
The definition of most specific is as follows: a method M is more specific than method N if the arguments to M can be passed to N without compilation error.