Tuesday, July 29, 2008

Encapsulation in OOP

Level: Basic

Two of the main features of the Object-Oriented Programming (OOP) paradigm is flexibility and maintainability. It is so desirable for one's source code to be flexible in spite of changes in its internal implementations. Programmers don't have to worry about their codes getting broken due to changes in the implementation of the Application Programming Interfaces (API) that they are using. All they have to know is that public methods of a given API behave the way they know it should regardless of its implementation details. Through this sense of flexibility in one's code, users of such API are guaranteed that their own code will behave accordingly for a very long time. This advantage is offered by OOP through encapsulation. Encapsulation hides the implementation details of an API to accomodate maintainability and flexibility in its use. It serves as a protection for a class's attributes by forcing client users of the class to access its protected attributes through publicly accessible methods or behaviors.
An example implementation of encapsulation in class would be the following (using the Java programming language):

public class Person {
private String name;
private String age;

public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}

public void setAge(int age){
this.age = age;
}
public int getAge(){
return this.age;
}
}


Notice that class Person's attributes are declared as private to protect them from being accessed directly outside Person. Instead, they are accessed through what we call setters (setName,setAge) and getters (getName, getAge). Clients of this class will have to use setters to set the value of its attributes and getters to determine the value of its attributes. By doing so, we will not be able to see others codes accessing Person's name and age as follows:

public class Tester {
void testPerson(){
Person person = new Person();
person.name = "Juan dela Cruz";
person.age = 20;
}
}


Instead, Tester will class will use Person class as follows:

public class Tester {

void testPerson(){

Person person = new Person();

person.setName"Juan dela Cruz");

person.setAge(20);

}

}


Also, since Tester class accesses Person's age through Person's setAge setter, we could be sure that Person's age will not have 0 or any negative value by adding a checker into Person's setAge method like the following:

//preceeding code removed
public void setAge(int age) throws
IllegalAgeValueException{
if (age > 0) {
this.age = age;
} else {
throw new IllegalAgeValueException(); //in which
IllegalAgeValueException is a //custom exception
}
}


Encapsulation in OOP leverages an API's maintainability and flexibility if used properly. Of course it doesn't mean that since OOP offers encapsulation the programmer does not have to do anything in order to take advantage of such feature. In order to do so, one has to write his code in an encapsulated manner. :]

Continually learning,
Jep

No comments: