Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Analyze the following code. What will be printed when this is executed? Justify

ID: 3813650 • Letter: A

Question

Analyze the following code. What will be printed when this is executed? Justify each of the output.


class Parent {

     void overload (Parent p) {

           System.out.println("Parent");

     }

} // end Parent


public class Child extends Parent {

     void overload (Child c) {

          System.out.println("Child overload");

     }

     void overload (Parent p) {

           System.out.println("Child override");

     }

     void testOverload (){

           Parent p = new Child();

           Child c = new Child();

           overload(p);

           overload(c);

           p.overload(p);

           c.overload(c);

           p.overload(c);

           c.overload(p);

     } // end testOverload


     public static void main(String[] args) {

           Child c = new Child();

           c.testOverload();

     } // end main

} // end Child

Explanation / Answer

The question is based on the RunTime Polymorphism concept of java.

The compiler decides which function to call at runtime.

The OUTPUT

Child override
Child overload
Child override
Child overload
Child override
Child override

overload(p); calls the function inside the child class because no object is involved. So, pretty simple. It searches for the ovcerload function which takes the parameter of the datatype same as of p. And since p is an object of class Parent so,

void overload (Parent p) {

           System.out.println("Child override");

     }

gets executed.

Next, overload(c); is called. again no object involved less complications. The current class' method is called but this time the parameter c is of type Child.So,

void overload (Child c) {

          System.out.println("Child overload");

     } is executed.

Next p.overload(p); is called. Now comes in the reference variable p which is of type Parent. But there is a catch, the object stored in the reference p is of type Child. Parent p = new Child(); p holds an object of class Child. So, the compiler only searches in the class Child for the method overload() with a parameter of datatype Parent because p is of type Parent, only the object inside p is of type Child.

THIS IS BECAUSE IN INHERITENCE, A PARENT CLASS CAN HOLD AN OBJECT OF THE CHILD CLASS BUT VICE-VERSA IS NOT TRUE.

So, like in the first example, the

void overload (Parent p) {

           System.out.println("Child override");

     }

is called because the compiler checks for the method with matching datatype of the parameter.

Next, c.overload(c); there is no use of Parent class whatsover because c is of type Child.  Child c = new Child();

so, again compiler checks for the method with parameter of Child type and

void overload (Child c) {

          System.out.println("Child overload");

     }

executes.

NOW IF YOU LOOK CAREFULLY, YOU WILL SEE THAT BOTH c AND p ARE OBJECTS OF CLASS Child. THE DIFFERENCE IS ONLY IN THE REFERENCE TYPE. SO, SINCE NO OBJECT IS CREATED FOR THE CLASS Parent. THEREFORE, THE overload() METHOD INSIDE THE CLASS Parent NEVER EXECUTES. YOU JUST HAVE TO SEE THE DATATYPE OF THE PARAMETER PASSED DURING CALL TO overload() AND CAN EXECUTE BLOCKS ACCORDINGLY.

HOPE THIS HELPS, IF NOT PLEASE FEEL FREE TO COMMENT

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote