Protected Data Members!

addam davis
2 min readJul 28, 2021


Objective: Privatize and/or protect variables of custom classes!

When you are using custom classes and inheritance, sometimes you don’t want them to be public outside of the inheritance. For example, if you were making a racing game, you wouldn’t want anything besides the player to be able to affect the speed variable.

If you were to switch the variable from public to private, then the child classes would no longer have access to the variables.

To keep access to the inherited classes but no one else, you use a variable type called “protected”

You can also use the protected type with methods.

Now you have you class privatized to only the base class and the child classes. It is up to you to decide if you want your variables to be protected or public. As always, never be afraid to experiment with your code, and I’ll see you in the next tutorial.