what is the advantage of declaring a reference variable as an interface type in JAVA?

Viewed 1199

I was reading this PowerPoint presentation when I came across this:

When you call a method through one of these references, the correct version will be called based on the actual instance of the interface being referred to. This is one of the key features of interfaces. The method to be executed is looked up dynamically at run time, allowing classes to be created later than the code which calls methods on them.

Can anyone explain me about this concept? I referred few websites and book and I am still not clear about how this concept works. From above line it is known that it calls correct version of method. How does it work and when should I cast an object to interface type.

1 Answers
Related