Public vs Private vs Protected — What's the Difference?
These access modifiers control which code can access a class's fields and methods: public members are accessible from anywhere, private members only from within the same class, and protected members from the class and its subclasses.
Published October 4, 2026
These access modifiers control which code can access a class's fields and methods: public members are accessible from anywhere, private members only from within the same class, and protected members from the class and its subclasses.
Why they differ
- Public exposes a member as part of a class's external contract, usable by any other code
- Private hides internal implementation details, letting them change freely without affecting other code
- Protected allows subclasses to reuse and extend internal behavior that outside code still can't touch
How to choose
- Default to private for internal state and helper methods, and expose only what's genuinely needed as public
- Use protected specifically when subclasses legitimately need access to internal details for extension, not just outside callers
FAQ
Does every language support all three?
Most mainstream OOP languages (Java, C#, PHP, C++) support all three. Some languages, like Python, only enforce this by convention (a leading underscore) rather than a strict compiler-enforced rule.