What is the difference between protected and private?

103,200

Solution 1

private - only available to be accessed within the class that defines them.

protected - accessible in the class that defines them and in other classes which inherit from that class.

Solution 2

Things that are private are only visible within the class itself.

Things that are protected are visible in the class itself and in subclasses.

Solution 3

The difference is who can access those functions.

  • Private = only members of the same class can access the function.

  • Protected = Same as private but derived classes can also access.

Solution 4

Private members can only be used by that classes members and its friends; protected members can be inherited by other classes, and can be used by the classes members and friends.

Solution 5

Private methods are usually visible to class instances (internal implementations), protected methods are visible to subclasses and classes in the same package (inheritance and restricted usage).

Share:
103,200
danijar
Author by

danijar

Researcher aiming to build intelligent machines based on concepts of the human brain. Website · Twitter · Scholar · Github

Updated on July 09, 2022

Comments

  • danijar
    danijar almost 2 years

    I don't understand the difference between protected and private members or methods, as I assumed both will hide the member or the function to access from outside the class.

    What is the difference between the protected and the private keywords?