158k views
5 votes
Select ALL statements which can replace / MISSING CODE / and compile without errors in the following method someMethod defined in the class C. Pay attention to access modifiers! public class A {

private int x; protected string s; }
public class B extends A {
private int y: protected boolean b;
} public class C extends B{
private int z;
public void someMethod (){
/* MISSING CODE */
}
}
a. this.x = 10; b. this.s = "p5"; c. this.b = true; d. this.y = 20; e. this.z - 30;

User Weston
by
7.6k points

1 Answer

5 votes

Answer:

public void someMethod() {

this.x = 10;

this.s = "p5";

this.z = 30;

}


This code assigns the value 10 to the private variable x inherited from class A, assigns the string "p5" to the protected variable s inherited from class A, and assigns the value 30 to the private variable z declared in class C.

Step-by-step explanation:

Option a. is valid because the variable x is inherited from class A and is a private member of class A, therefore it can only be accessed within class A, including its subclasses such as B and C. Since class C extends class B and class B extends class A, this.x can be accessed and modified in class C.

Option b. is valid because the variable s is inherited from class A and is a protected member of class A, which means it can be accessed by its subclasses such as B and C. Therefore, this.s can be accessed and modified in class C.

Option c. is invalid because the variable b is a protected member of class B, which means it can only be accessed within class B or its subclasses. Class C can access b directly, but it is not inherited from class A, so this.b is not a valid statement in class C.

Option d. is invalid because the variable y is a private member of class B, which means it can only be accessed within class B, but not by its subclasses such as C. Therefore, this.y is not a valid statement in class C.

Option e. is invalid because it has a typo and is also an expression rather than an assignment statement. The correct version of option e should be this.z = 30; if we want to assign the value 30 to the variable z in class C.

User Bestter
by
7.9k points