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

Draw the MEMORY DIAGRAM for the following question (AND MAKE SURE THAT YOU show

ID: 3850599 • Letter: D

Question

Draw the MEMORY DIAGRAM for the following question (AND MAKE SURE THAT YOU show me the previous values even after updating them using an arrow or crossing out the previous value), so that I can keep track of whats going on!! AND give the final output. FROM JAVA.

Also, please explain what is the point of the variables and methods being protected/private?

private int num1: grotected int num3; Base (int num) num 1 num; num2 num 1 num3 num 2: public String toString0 return "(base) num1 num2 num3 protected void incr (int num) num 1 num num2 class Sub1 extends Base private int num4 protected int num5 Sub1 (int num) super (num); num 4 num2 num: num5 num3 num; public String toString() return super toString() (Sub1 num4 num5 class Sub2 extends Sub private int num2 Sub2 (int num) super(num 1): num 2 num; num5 num 2 public String toString() return super toString0 (Sub2) num2; public void incr(int val) num2 val. num5 Vali super incr(val); public class PolyExercise public static void main (String args) Base obj1 new Base (1) Sub1 obj2 new Sub1(3) Sub2 obj3 new Sub2(4); System.out.println (obj1); System.out.println (obj2): System.out.println (obj3); obj incr (1) adl to num obj2.incr (2); obj3 incr (3); system out printin (obj1) o System out.println (obj2)HI System.out printin (ob 3)

Explanation / Answer

Output:

(base)1 2 3
(base)3 4 4(Sub1)7 8
(base)5 6 6(Sub1)11 6(Sub2)4
(base)2 3 3
(base)5 5 4(Sub1)7 8
(base)8 7 6(Sub1)11 9(Sub2)7

Memory Diagram

class Base

Base obj1 = new Base(1)

Base

num1 = 1

num2 = 2

num3 = 3

hence (base) 1 2 3 is displayed by toString() method

class Sub1

Sub1 obj2 = new Sub1(3)

super(3) --- passing 3 to Base class constructor

Base

num1 = 3

num2 = 4

num3 = 5

Sub1

num4 = 7

num5 = 8

num3 = 4(changed)

obj2.toString() will display super.toString() + "(Sub1)" + num4 +" "+num5;

(base) 3 4 4 (Sub1) 7 8

class Sub2

Sub2 obj3 = new Sub2(4)

Base

num1 = 5

num2 = 6

num3 = 7

Sub1

num4 = 11

num5 = 12

num3 = 6

Sub2

num2 = 4

num5 = 6

Hence the output (base)5 6 6(Sub1)11 6(Sub2)4

Incrementation of all objects by calling incr() function

obj1.incr(1);
   obj2.incr(2);
   obj3.incr(3);
        

class Base

Base

num1 = 2

num2 = 3

num3 = 3

hence (base) 2 3 3 is displayed by toString() method

class Sub1

obj2.incr(2)

Base class incr(3) will work

Base

num1 = 3+2 = 5

num2 = 4 ++ = 5

num3 = 4

Sub1

num4 = 7

num5 = 8

obj2.toString() will display super.toString() + "(Sub1)" + num4 +" "+num5;

(base) 5 5 4 (Sub1) 7 8

class Sub2

obj3.incr(4)

Base

num1 = 5+3 = 8

num2 = 6++ = 7

num3 = 6

Sub1

num4 = 11

num5 = 12

num3 = 6(changed)

Sub2

num2 = 10

num5 = 9(changed)

Hence the output (base)8 7 6(Sub1)11 9(Sub2)7

Protected variables are accessible to derived classes but private variables are not accessible to derived or any other class. private variables are accessible inside the class only.

Base

num1 = 1

num2 = 2

num3 = 3

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