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

Question 4-2 Fix the following program to produce the output. You SHOULD NOT CHA

ID: 3876350 • Letter: Q

Question

Question 4-2 Fix the following program to produce the output. You SHOULD NOT CHANGE/ADD any statement in the MAIN method. AND explain why this program is not working at this moment, and how you fixed the errors in DETAIL at the bottom of the program. class A4 public String x ) (return "Ax"; class B4 extends A4 int i = 2; public string y () freturn "Bay";) public string z ) treturn "B.z." public class PolymorphismWhy4( public static void main(String[] args) [ A4 obj = new B4(); System.out.println (obj.i); Sys (obj-X( ) ) ; (obj.y.)) // output System.out.println (ob J.Z.( ) ) ; Ax / Explain why and how in detail here

Explanation / Answer

Updated program: (change obj type from A4 to B4)

public class PolymorphismWhy4 {

   /**
   * @param args
   */
   public static void main(String[] args) {

      
// A4 obj = new B4(); //change obj type from A4 to B4
       B4 obj = new B4();
       System.out.println(obj.i);
       System.out.println(obj.x());
       System.out.println(obj.y());
       System.out.println(obj.z());
   }
}
/*   A4 obj = new B4(); this statement follows Dynamic binding means the object type could be determined at runtime.
*    obj can't access the properties and methods of B4(Derived class) because obj acts as the instance of A4 (Base class)
*   
*    Hence, change the obj type to B4 as shown below:
*    B4 obj = new B4();
*
*/

OUTPUT: ( i value could never be 1 as per the program )

2
A.x
B.y
B.z

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