Encapsulation
easy 35 minPut the data and the methods that work on it into one unit, and let outside code reach the data only through those methods.
Watch lesson4:51
Where it shows up
The same pattern appears across everyday software.
Java `String`, JEP 254
Java 9 replaced the char array inside by a byte array and a flag. The proposal calls it purely an implementation change, with no changes to existing public interfaces.
SQLite connection
Programs hold a pointer to `sqlite3`, which the documentation calls an opaque structure. It also says it is useful to think of the pointer as an object: open functions are its constructors.
Go packages
An identifier is exported when its name starts with an upper case letter. Packages below a directory called `internal` cannot be imported from outside the module.
Rust modules
By default everything is private, with two exceptions. `pub` opens an item, `pub(crate)` opens it inside one crate only.
Kotlin `internal`
A fourth level beside public, protected and private: visible everywhere inside the same module, and not outside.
C# properties
They look like public data members and are special methods called accessors. A getter and a setter may have different visibility.
Node.js package `exports`
A package lists its entry points. The documentation calls this encapsulation: other files of the package can no longer be imported from outside.
Python `property`
Turns an attribute into a managed attribute with functions for get, set and delete. Callers keep writing `x.name`.
Kinds of systems
Bank accounts, password checks (`validate(password)` and no password getter), game characters with health limits, sessions in mobile apps, shopping carts that keep their own total, and patient records with authorized access only.