Below are four classes – classA, classB, classC, and classD. ClassA, ClassB, and
ID: 3759906 • Letter: B
Question
Below are four classes – classA, classB, classC, and classD. ClassA, ClassB, and ClassC are in the same package “one”. ClassD is in package “two”. Packages “one” and “two” are in the same folder. ClassC and classD are derived from classA. ClassB, classC, and classD have exactly one public method. Within the test methods, there are some JAVA statements. Indicate which statements are allowed (write a ‘Y’ next to the statement) and indicate which ones will give compilation error (write a ‘N’ against the statement).
package one;
public class classA{
public int x;
private int y;
protected int z;
int v;
public void sety(int yy)
{ y = yy; }
}
package one;
public class classB{
private classA a_obj;
public void testB()
{
a_obj.x=1;
a_obj.y=2;
a_obj.z=3;
a_obj.v=4;
a_obj.sety(7);
}
}
package one;
public class classC extends classA {
public void testC()
{
x = 1;
y = 2;
z = 3;
v = 4;
sety(6);
}
}
package two;
import one.classA;
public class classD extends classA{
public void testD()
{
x = 1;
y = 2;
z = 3;
v = 4;
sety(7);
}
}
Explanation / Answer
package one;
public class classA{
public int x;
private int y;
protected int z;
int v;
public void sety(int yy)
{ y = yy; }
}
package one;
public class classB{
private classA a_obj;
public void testB()
{
a_obj.x=1;
a_obj.y=2; // 'N' y has private access in classA
a_obj.z=3;
a_obj.v=4;
a_obj.sety(7);
}
}
package one;
public class classC extends classA {
public void testC()
{
x = 1;
y = 2; // 'N' y has private access in classA
z = 3;
v = 4;
sety(6);
}
}
package two;
import one.classA;
public class classD extends classA{
public void testD()
{
x = 1;
y = 2; // 'N' y has private access in classA
z = 3;
v = 4; //'N' v is not public in classA, therefore cannot be accessed from outside the package
sety(7);
}
}
apart from the 'N' statements, rest all of them are 'Y' ie; valid.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.