Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

If a parent class has a private variable and has no accessors for it, what would

ID: 3907013 • Letter: I

Question

If a parent class has a private variable and has no accessors for it, what would be the best solution if a child class needed read only access to it? Best solution Allowing the child class to perform only the required (no additional optional) operations while preventing non-child classes access to it. O Declare the variable as private Declare the variable as protected O Declare the variable as public O Declare a public property for the variable O Declare a protected property for the variable O Declare a private property for the variable Declare a public property for the variable with a private set O Declare a protected property for the variable with a private set O Declare a private property for the variable with a private set Declare a public property for the variable with a protected set Declare a protected property for the variable with a protected set Declare a private property for the variable with a protected set

Explanation / Answer

DECLARE A VARIABLE AS PROTECTED

the variable which has private access modifier cannot be accessed outside the class whether it is inherited to a child class. the default access modifier gives that permission, it is always better to mark a variable private or protected rather than default but in the current case if the variable needs to be get inherited in child class, than for that case the modifier must be declared to protected.

Private members exists in the instance of a child class, therefore the object of sub class is also an object of super class, so they are accessible indirectly with the help of non private method of super class. this can access and manipulate the private members  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote