Which statements are most accurate regarding the following classes? class A { private int i; protected int j; } class B extends A { private int k; protected int m; // some methods omitted } Group of answer choicesIn the class B, an instance method can only access i, j, k, m.In the class B, an instance method can only access j, k, m.n the class B, an instance method can only access j, m.In the class B, an instance method can only access k, m.
Question
Which statements are most accurate regarding the following classes? class A { private int i; protected int j; } class B extends A { private int k; protected int m; // some methods omitted } Group of answer choicesIn the class B, an instance method can only access i, j, k, m.In the class B, an instance method can only access j, k, m.n the class B, an instance method can only access j, m.In the class B, an instance method can only access k, m.
Solution
In the class B, an instance method can only access j, k, m.
Here's why:
-
iis a private member of class A. Private members are only accessible within the class they are declared. So,icannot be accessed directly from class B or any other class. -
jis a protected member of class A. Protected members are accessible within the same package and also in the subclasses, even if they are in a different package. So,jcan be accessed from class B. -
kandmare members of class B. Any member of a class can be accessed within the same class. So,kandmcan be accessed from class B.
Similar Questions
Which statements are most accurate regarding the following classes? class A { private int i; protected int j; } class B extends A { private int k; protected int m; } Group of answer choicesAn object of B contains data fields i, j, k, m.An object of B contains data fields j, k, m.An object of B contains data fields j, m.An object of B contains data fields k, m.
Which of the following statements is true regarding protected members in inheritance? (2 Points) a) Protected members can only be accessed by the derived class. b) Protected members can be accessed by any class in the same namespace. c) Protected members can be accessed by any class derived from the base class. d) Protected members can be accessed by any class in the program.
Which of the following statement/s are correct?I. The access modifier “private” can be used with every class.II. Default access level is less restrictive than the protected access level.III. The only access modifier which can be used inside a method is public.*Only II.Only III.Only I and II.Only I and III.None of above
Given these class definitions, which of the following must occur within class S?public abstract class R{ private int a; protected double c; public abstract void doThis();}public class S extends R{}a.The doThis() method must be definedb.An accessor method must be created for instance variable ac.An accessor method must be created for instance variable c
Which of the following is false about protected class members?They begin with one underscoreThey can be accessed by subclassesThey can be accessed by name mangling methodThey can be accessed within a class
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.