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

 

Online Tutors USA United States Get To Know Us Get in Touch   Tutoring Services   Top Subjects Test Prep
7740 McCallum Blvd, Dallas, TX About Us Contact Us   K-12 Tutoring   English Tutor GMAT Tutors
info@tutorspoint.com How It Works FAQ   Homework Help   Math Tutor LSAT Tutors
STUDENT SUPPORT Terms of Use Become a Tutor   Test Prep   Science Tutor SAT Tutors
support@tutorspoint.com Privacy Policy Students Blog   Assignment Help   Computer Tutor MCAT Tutors