3. (10 pts) Given the following Java code: class C public int foo (C p) return 1
ID: 3600389 • Letter: 3
Question
3. (10 pts) Given the following Java code: class C public int foo (C p) return 1; class D extends C public int foo (C p) return 2; ) public int foo(D p)f return 3; C pnew CO C qnew DO D rnew DO int i-p.foo(r) int j = q. foo (q) ; int k-q.foo (r) (Remember that in Java every object is accessed through a pointer and that methods are virtual by default.) Which methods get called in the three calls, i.e., what are the values of i, j, and k? Explain how the method selection works to result in these values.Explanation / Answer
i =1
Explanation:- In this case the foo method in class C is called
j = 2
Explanation:- In this case the reference to the object created by new D(); is stored in q. And the first method in class D which accepts Objects of Class C is called
k =3
Explanation:- In this case the reference to the object created by new D(); is stored in q. And the second method in class D which accepts Objects of Class D is called
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.