Co-Authored By:
Asked by: Nurul Larraza
technology and computing programming languagesCan objects of a class access protected members?
Simply so, can objects access protected members?
Protected members (C++ only) A protected nonstatic base class member can be accessed by members and friends of any classes derived from that base class by using one of the following: An object of a directly or indirectly derived class.
Subsequently, question is, how can a protected member can be accessed?
Protected data member and method are only accessible by the classes of the same package and the subclasses present in any package. You can also say that the protected access modifier is similar to default access modifier with one exception that it has visibility in sub classes. Classes cannot be declared protected.
Its only difference occurs in fact with inheritance: When a class inherits another one, the members of the derived class can access the protected members inherited from the base class, but not its private members. With protected , all public members of the base class are inherited as protected in the derived class.