Sunday, July 8, 2012

Essence of OOP: Runtime Polymorphism in Java

This post does not give the definitions of Runtime polymorphism or Dynamic method dispatch. Instead, it helps Java beginners (or any OOP Beginner) to use runtime polymorphism. I personally think Inheritance and Runtime polymorphism forms the crust of OOP (not just in Java but in all other OOP Languages also).

The entire dynamic method dispatch is based on the rule that an object reference of the super class can refer to an object of any subclass in the hierarchy. This can be explained with the following example.

Consider there are two classes Person and Faculty. Person class is the super class of Faculty class.

Since superclass and subclass share IS A relationship, every Faculty is a Person. So, whenever Java expects Person, a Faculty can be used. This kind of implicit casting is called upcasting.



Unlike in upcasting, every Person need not be a Faculty. Thus, if we use Person in place of Faculty, explicit casting needs to be done. Therefore, we need to use explicit typecasting in order to perform this operation. This kind of explicit casting is called as downcasting.



In runtime polymorphism, the call to an overridden method is resolved at runtime instead of compile time. It occurs when an overridden method is called using an object reference of superclass. Remember that you can only call methods which are present in the super class.

Now let us extend the previous mentioned example with another subclass to "Person" class: NonTeaching class. Now the class diagram is as follows:

The below class demonstrates the runtime polymorphism for the above class diagram.



As can be seen from the code above, Java calls appropriate method depending on the object pointed to by the object reference. In the first case, the object reference p is pointed to the object of Faculty class. So, the printDetails() from the Faculty class is called. In the second case, p points to object of NonTeaching class. Thus, the printDetails() from the NonTeaching class is called.

Always remember that the dynamic method dispatch is applied only to method that is defined in the super class, overridden in subclass and invoked using an object reference of superclass.

14 comments:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai

    ReplyDelete
    Replies
    1. Thanks for your comment. I don't get enough time to blog these days. Sorry about this. I will however try to add more posts.

      Delete
  2. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing..
    Are you looking for the best Java training institute in Chennai?
    Come and learn with Aorta, the best java training institute in Chennai offering the best platform to learn and get the depth of Java
    java training in chennai
    java course in chennai

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...