A major principle of OO programming is keeping the implementation of an object separate from manipulation. This article explains how that works.
A major principle of Object-Oriented programming is keeping the implementation of an
object separate from manipulation. Let’s look at an example in Java:
Notice how section 1 contains the instance variables. These are also called attributes, and they contain information that an object of this class will hold—In this case, customer information. In this example, all instance variables are private, meaning that they can only be directly accessed by an object of type customer.
Sections 2 and 3 contain public methods. Methods are the behaviors associated with a class. Notice that all of these methods are public, meaning that any other object can call them. If they were private, they would only be able to be called from other methods inside of an object of this class. Section 2 contains setters, which are used to change the values of the instance variables. Section 3 contains getters, which are used to get the value of an instance variable. Within each of the setters and getters, a programmer could convert from one type to another. For example, if name was stored as an array of characters, the setter could accept a string as a parameter and then convert this to an array of characters which would be stored in the instance variable.
So, in our example, a programmer who does not have access to the customer source code only needs to know how to get access to the instance variables and to change these through the getters and setters. He or she does not need to know how each instance variable is stored. If a change to a data type of an instance variable was needed, this could be very easily done, and the getter and setter for that variable would be modified. A programmer responsible for maintaining objects that use the customer object would not need to rewrite any code for these objects, since the getters and setters would still behave the same way. This is the concept of decoupling: separating implementation from manipulation.
Source: Saylor Academy
This work is licensed under a Creative Commons Attribution 4.0 License.