enfrptes

sendassignment@tutorspoint.com

Super class reference and sub-class object rule

What are the Superclass reference and sub-class object rule?

When we call a function of an object through a reference, first JVM checks whether the function is there in JVM or not.

If the function is present in the object then the JVM searches whether the object has any sub-object or not,

if the object has a sub-object JVM searches whether the sub-object has any other further sub-objects. In this way, it reaches the sub-most objects. Now it finds the function in the sub-most objects. If it is found, the function is executed if not control searches for the function in the immediate superclass object, and so on.

When we call a function of an object through a reference, first JVM checks whether the function is present in the object or not, if the function is not present in the object, the JVM searches for the function in the immediate superclass object. In this way, if the function is not found even in the super most objects then it generates a compilation error.

If the function is found in either the pointed object or any of the super objects, then the JVM searches whether the object has any sub-object or not, if the object has a sub-object JVM searches whether the sub-object has any other further sub-objects. In this way, it reaches the sub-most objects. Now it finds the function in the sub-most objects. If it is found, the function is executed if not control searches for the function in the immediate superclass object, and so on.

When we call a function that is overridden in three classes via Class A, Class B, and Class C. where B extends A, C extends B then according to our discussion the overridden function in the sub most object gets the highest priority and it is executed first