Skip to main content

17.5) Public And Private


Encapsulation and information hiding are key ideas in object-oriented programming. These terms refer to the fact that the instance variables needed to describe an object are bundled together and made private, which prevents the surrounding software environment from accessing them directly.

Because instance variables are private and direct access isn’t possible, programs cannot accidentally tamper with object state and we can be more confident that the state is always valid.

The software environment within which an object resides can manipulate that object only by calling the methods identified in the public section of the class definition. These methods are the public interface of the object. Provided that the interface doesn’t change, we can freely alter the internal implementation details of a class without breaking any code that uses the class.