When using an object oriented programming paradigm, objects encapsulate only local data, which is by default accessible only by the object itself.
Rather than having to think about data and code as two separate concepts, an object oriented program merges the two in the concept of an object.
This increases understanding (analysts and programmers can consider objects of interest without internalizing
the workings of the complete program) and ease of maintenance. To realize the latter,
object-oriented programming applies two concepts known as 1) encapsulation
and 2) information hiding. One of the greatest sources of errors in programs is when some parts of the program are interfering with other parts.
The addition of more procedures and data will quickly lead to unstructured code, where it difficult to follow the trace of execution as data can jump from one part to another in the program.
Object-oriented programming resolves this issue by encapsulating of the data and behavior within an object.
However, this in itself is not sufficient to guarantee maintainable programs, as you also need to prevent the data of an object from being directly accessible by other objects. Therefore, object-oriented programming also emphasizes the concept of information hiding, where an object's data can by default be accessed only by methods contained in the same object.
When data elements of one object need to be used by another object, the latter must call a publicly accessible method,
, basically requesting the "owning object" to perform a change to its data (setter). As such, object-oriented programming encourages programmers to place data where it is not directly accessible(getter) or modifiable by the rest of the system.
Instead, the data is accessible through methods, which can also include checks to make sure the requested
change is permitted by the owning object. Object-oriented programming also defines concepts to help with structuring programs so that they can be easily extended and evolved. These concepts are 1) polymorphism, which is the ability to treat objects of different types in a similar manner, and 2) inheritance, which is a concept to allow for extending objects and enabling code reuse.