Select true statements about public, protected and private attributes/methods. (select three)

Questions & AnswersCategory: PythonSelect true statements about public, protected and private attributes/methods. (select three)
Lokesh Kumar Staff asked 2 years ago

Select true statements about public, protected and private attributes/methods. (select three)
a. Public attributes/methods are accessible outside the class.
b. Protected attributes/methods are accessible from within the class and are also accessible from its subclasses. No other classes have access to them. This allows certain resources of the parent class to be inherited by the child class. The convention used to protect an attribute/method is to prefix it with _ (single underscore).
c. Private attributes/methods are available outside the class.
d. Python doesn’t have any mechanism that effectively restricts access to any instance attribute or method. The recommended convention is to prefix an attribute/method name with double underscores to make the variable private.
e. Private attributes/methods are available from the class level and are also available from its subclasses. No other classes have access to them.

1 Answers
Lokesh Kumar Staff answered 2 years ago

a. Public attributes/methods are accessible outside the class.

b. Protected attributes/methods are accessible from within the class and are also accessible from its subclasses. No other classes have access to them. This allows certain resources of the parent class to be inherited by the child class. The convention used to protect an attribute/method is to prefix it with _ (single underscore).

e. Private attributes/methods are available from the class level and are also available from its subclasses. No other classes have access to them.

Explanation:

Public attributes (generally methods declared in a class) are accessible from outside the class.
Protected members of a class are accessible from within the class and are also available to its sub-classes.
Private. Python doesn’t have any mechanism that effectively restricts access to any instance variable or method. Python prescribes a convention of prefixing the name of the variable/method with a single or double underscore to emulate the behavior of protected and private access specifiers.