OO Abstraction arises during the development of a class level in order to hide the complexity of the implementation as far as the functions offered by the API / design / system are understandable, in a way simplifying the "interface" for accessing the basic implementation.
This process can be repeated with more and more "higher" levels (levels) of abstraction, which allows you to build large systems without increasing the complexity of the code and understanding at higher levels.
For example, a Java developer can use the high-level functions of FileInputStream without worrying about how it works (for example, files, file system security checks, memory allocation, and buffering will be managed internally and hidden from consumers). This allows you to change the implementation of FileInputStream , and as long as the API (interface) to FileInputStream remains unchanged, code built against previous versions will still work.
Similarly, when designing your classes, you will want to hide the internal implementation from others as much as possible.
In the definition of Booch 1 , OO Encapsulation is achieved by hiding information , and especially around hiding internal data (fields / members representing state) belonging to an instance of the class, by forcing access to internal data in a controlled manner and preventing direct external change of these fields, as well as hiding any internal methods for implementing the class (for example, making them private).
For example, by default, class fields can be made private , and only if external access to them is required, will get() and / or set() (or Property ) from the class be displayed. (In modern OO languages, fields can be marked as readonly / final / immutable , which further limits the change even inside the class).
An example where NO information hiding was applied (Bad Practice):
class Foo {
An example of using field encapsulation:
class Bar { // Improvement - access restricted only to this class private int encapsulated; // The state of Bar (and its fields) can now be changed in a controlled manner public void setEncapsulatedField(int newValue) { if (newValue < 100) { encapsulated = newValue; } // else throw ... out of range } }
An example of initializing an immutable / constructor field:
class Baz { private final int onlyValue; public void Baz(int immutableValue) {
Re: Abstraction against an abstract class
Abstract classes are classes that facilitate the reuse of commonality between classes, but which themselves cannot be directly created using new() - abstract classes must be subclasses, and only concrete (non-abstract) subclasses can be created. Probably one of the sources of confusion between Abstraction and the abstract class was that in the early days of OO, inheritance was more actively used for code reuse (for example, with associated abstract base classes). Currently, encapsulation composition is usually used in a more general sense when determining which methods, fields, properties, events, and other bundle in a class.
Quote from Wikipedia:
In a more specific setting of an object-oriented programming language, the concept is used as meaning either a mechanism for hiding information, a linking mechanism, or a combination of the two.
For example, in the statement βI encapsulated the data access code in my own class,β the interpretation of encapsulation is roughly equivalent to the Separation of Concern (S in SOLID) and, perhaps, can be used as a synonym for refactoring.
[1] Once you see Booch's cat image encapsulation , you can never forget encapsulation - p46 object-oriented analysis and design with applications, 2nd Ed