I know how to achieve runtime polymorphism through inheritance and through interfaces. But why do we need to have runtime polymorphism in the first place? I found a good explanation at Stack overflow :
http://stackoverflow.com/questions/2080020/what-is-the-real-significanceuse-of-polymorphism
Basically, interfaces allow you to use methods of a class, without getting into the details of the implementation(encapsulation?). This is achieved by using a reference of the interface to create an object of the implementing class. Now, all we need to do is call the methods of the class through the interface. This is where runtime polymorphism kicks in - determining the object whose method is to be called during runtime, instead of compile time.
DBOperation dbo = null; //DBOperation is an interface
if(system == "RDBMS")
dbo = new RDBMS(); //RDBMS implements DBOperation
else
dbo = new FileDB(); //FileDB implements DBOperation
dbo.openConnection(".....");
....
Here, the compiler does not know which object to instantiate, i.e., RDBMS() or FileDB(). It is decided during runtime, hence the concept of runtime polymorphism.
Now, back to why we need it.
As can be seen from the above example, when we have multiple implementations, and are not concerned about the actual implementation, but only want the functionality, we can define interfaces to exploit this functionality. This is most obvious in the List interface in Java.
"One other important use is, if you are using java, most of the time you would work on List interface, so that you can use ArrayList today or some other interface as your application grows or its needs change." - StackOverflow.
http://stackoverflow.com/questions/2080020/what-is-the-real-significanceuse-of-polymorphism
Basically, interfaces allow you to use methods of a class, without getting into the details of the implementation(encapsulation?). This is achieved by using a reference of the interface to create an object of the implementing class. Now, all we need to do is call the methods of the class through the interface. This is where runtime polymorphism kicks in - determining the object whose method is to be called during runtime, instead of compile time.
DBOperation dbo = null; //DBOperation is an interface
if(system == "RDBMS")
dbo = new RDBMS(); //RDBMS implements DBOperation
else
dbo = new FileDB(); //FileDB implements DBOperation
dbo.openConnection(".....");
....
Here, the compiler does not know which object to instantiate, i.e., RDBMS() or FileDB(). It is decided during runtime, hence the concept of runtime polymorphism.
Now, back to why we need it.
As can be seen from the above example, when we have multiple implementations, and are not concerned about the actual implementation, but only want the functionality, we can define interfaces to exploit this functionality. This is most obvious in the List interface in Java.
"One other important use is, if you are using java, most of the time you would work on List interface, so that you can use ArrayList today or some other interface as your application grows or its needs change." - StackOverflow.