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

Consider the following two classes: public class SuperClass{ public int A(){ ret

ID: 3728445 • Letter: C

Question

Consider the following two classes:

   public class SuperClass{

       public int A(){

           return A() + 3;

       }

       public int B(){

           return 7;

       }

   }

   public class SubClass extends SuperClass{

       public int A(){

           return super.B() + 2;

       }

       public int B(){

           return super.A() + 5;

       }

       public int C(){

           return super.A();

       }

   }

   Assume that the following declaration appears in a client program:

   SuperClass x = new SubClass();

   What is printed as a result of the call to System.out.print(x.B())?

   (A)   8

   (B)   12

   (C)   15

   (D)   17

   (E)   Nothing is printed because of infinite recursion.

Explanation / Answer

Explanation

x.B() is called
super A + 5
(sub A + 3) + 5
((super B + 2) + 3) + 5
7 + 2 + 3 + 5 = 17

(D)   17