Wednesday, 1 June 2016

Reference variable type, actual Object type and Polymorphism

What is Polymorphism in Java?  literally, polymorphism  means many forms. Different living being may have the same behaviors, but react differently according to their own. A lion and a bird are both living beings, but they eat in different manners.

Just like in a real life, Java provides a mechanism to model this phenomena, and implemented by an way of method overriding in an inheritance hierarchy. this also incur the run-time method selection of object type refer to the reference variable type.

The tricky part maybe here, a reference variable could be declared as its base class or implemented interfaces,  however its instantiated instances could have different forms modeled by its sub-classes. I think, this is also a kind of so called polymorphism.  Declaring in a generic form, a super type, but it could be implemented by different concrete sub-types.

However, a critical concept of the polymorphism is about the method overriding. Super-class's instance methods can be inherited by the child classes, but where they can be overridden. So, for a super-class form, it not only could have different forms of run-time objects, but also their same methods can have different forms.

As overriding happens, invoking a method via a reference variable types, it needs to watch out which instance method has been pointed to. This depends on reference variable's run-time object.

Please note: an overridden method, just like its literal meaning, in the sub-class scope its super-class one has been destroyed. On this case, even for the super-class, it have to invoke its sub-class overridden one.













No comments:

Can Jackson Deserialize Java Time ZonedDateTime

Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...