Question
package testpkg.p1;
public class ParentUtil
{
public int x = 420;
protected int doStuff() { return x; }
}
package testpkg.p2;
IMPORT testpkg.p1.ParentUtil;
public class ChildUtil extends ParentUtil
{
public static void main(String [] args)
{
new ChildUtil().callStuff();
}
void callStuff()
{
System.out.print("this " + this.doStuff() ); /* Line 18 */
ParentUtil p = new ParentUtil();
System.out.print(" parent " + p.doStuff() ); /* Line 20 */
}
}
package testpkg.p1;
public class ParentUtil
{
public int x = 420;
protected int doStuff() { return x; }
}
package testpkg.p2;
IMPORT testpkg.p1.ParentUtil;
public class ChildUtil extends ParentUtil
{
public static void main(String [] args)
{
new ChildUtil().callStuff();
}
void callStuff()
{
System.out.print("this " + this.doStuff() ); /* Line 18 */
ParentUtil p = new ParentUtil();
System.out.print(" parent " + p.doStuff() ); /* Line 20 */
}
}
which statement is true?
Answer: Option C
Was this answer helpful ?
The ParentUtil instance p cannot be used to access the doStuff() method. Because
doStuff() has protected access, and the ChildUtil class is not in the same package as the ParentUtil class, doStuff()can be accessed only by instances of the ChildUtil class
(a subclass of ParentUtil).
Option A, B and D are incorrect because of the access rules described previously.
Was this answer helpful ?
Submit Solution